ho salato e hash i miei dati di accesso according to this Code Project articlepassword hashing utilizzato per il lavoro, ora non posso entrare nel mio software
Quando ho fatto questo, la password e il sale campi del database erano colonne appena varchar. I dati sono stati visualizzati in SQL Server Management Studio come punti interrogativi.
Un amico è arrivato e ha cambiato le colonne in tipo di dati nvarchar e ora i dati sembrano essere una raccolta di lettere/parole asiatiche - presumo che non abbia alcun senso alle spalle nemmeno a qualcuno che legge qualunque lingua sia.
Il problema è che ora, anche quando ho creato un nuovo utente, ogni tentativo di accesso non riesce.
Inoltre l'utente ed effettuare il login
public User Login() // returns an instance of its own class
{
// get the salt value for the user
var auser = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0);
if (auser == null)
{
throw new ValidationException("User not found");
}
// hash the login for comparison to stored password hash
HashGenerator h = new HashGenerator(password, auser.usersalt);
string hash = h.GetHash();
var us = ie.users.FirstOrDefault(u => String.Compare(u.emailaddress, emailaddress, false) == 0 && String.Compare(u.password, password, false) == 0);
if (us == null)
{
throw new Exception("Invalid email address and/or password."); // seems here's where it goes wrong
}
User user = new User();
user.userid = us.userid;
user.storeid = us.storeid;
user.role = us.role;
return user; // login succeeded, return the data to the calling method
}
public void Add()
{
user newuser = new user();
newuser.storeid = storeid;
newuser.emailaddress = emailaddress;
// Generate password hash
string usersalt = SaltGenerator.GetSaltString();
HashGenerator hash = new HashGenerator(password, usersalt);
newuser.password = hash.GetHash();
newuser.role = role;
newuser.usersalt = usersalt;
ie.users.Add(newuser);
ie.SaveChanges();
}
Hash e Salt Generator
public class HashGenerator
{
public string pass { get; protected set; }
public string salt { get; protected set; }
public HashGenerator(string Password, string Salt)
{
pass = Password;
salt = Salt;
}
public string GetHash()
{
SHA512 sha = new SHA512CryptoServiceProvider();
byte[] data = CryptUtility.GetBytes(String.Format("{0}{1}", pass, salt));
byte[] hash = sha.ComputeHash(data);
return CryptUtility.GetString(hash);
}
}
public static class SaltGenerator
{
private static RNGCryptoServiceProvider provider = null;
private const int saltSize = 128;
static SaltGenerator()
{
provider = new RNGCryptoServiceProvider();
}
public static string GetSaltString()
{
byte[] saltBytes = new byte[saltSize];
provider.GetNonZeroBytes(saltBytes);
return CryptUtility.GetString(saltBytes);
}
}
utilità Crypt per ottenere le stringhe e byte
class CryptUtility
{
public static byte[] GetBytes(string Str)
{
byte[] bytes = new byte[Str.Length * sizeof(char)];
System.Buffer.BlockCopy(Str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
public static string GetString(byte[] Bytes)
{
char[] chars = new char[Bytes.Length/sizeof(char)];
System.Buffer.BlockCopy(Bytes, 0, chars, 0, Bytes.Length);
return new string(chars);
}
}
L'accesso utilizzato per funzionare e questo codice non è cambiato da allora ... L'unica cosa che è stata modificata è che la password e le colonne salate nella tabella utenti sono ora nvarchar
anziché varchar
.
Detto questo, ho pensato di creare un nuovo utente con il metodo Add sopra, ma l'accesso con l'utente viene a mancare pure.
Sono veramente in perdita quanto a come risolvere questo problema. Non posso procedere allo sviluppo del resto del sistema se non riesco ad accedervi per testare.
Ogni aiuto sarà molto apprezzato!
Hai provato cambiando le colonne torna a 'varchar'? – Tim
La conversione indietro dà un avvertimento sulla possibile perdita di dati, quindi non – Ortund
Fare una copia del database, eseguire la conversione su 'varchar' e vedere se funziona. Molto bene potrebbe essere la perdita di dati, ma l'intero sforzo dovrebbe essere piuttosto veloce e potrebbe funzionare. – Tim