2013-10-30 9 views
18

Possiedo un sito MVC 4 che attualmente implementa SimpleMembership. Nella prossima iterazione del sito mi piacerebbe aggiornare in MVC 5 e identità ASP.NET. Entrambi i siti hanno la stessa chiave macchina in web.config. I tavoli SimpleMembership SQL hanno una colonna per passwordePaswordSalt tabelle ASP.NET identità hanno una colonna per PasswordHash che sembra essere una combinazione di password + PasswordSalt.Migrazione di utenti esistenti da MVC 4 SimpleMembership a MVC 5 Identità ASP.NET

ho provato concatenando il password e PasswordSlat insieme del vecchio sito, ma questo non funziona.

La mia domanda è,

come faccio a migrare le password degli utenti esistenti del vecchio sito al nuovo sito?

+2

Esiste un tutorial [qui] (http://kevin-junghans.blogspot.com/2014/02/migrating-existing-website-from.html). – JonasCz

+0

E questo qui http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity – Bellash

risposta

1

Secondo http://www.asp.net/identity/overview/migrations/migrating-an-existing-website-from-sql-membership-to-aspnet-identity:

Le password degli utenti dell'applicazione vengono crittografate e memorizzate nel database. L'algoritmo crittografico utilizzato nell'appartenenza SQL è diverso da quello nel nuovo sistema Identity. Per riutilizzare le vecchie password, è necessario decodificare selettivamente le password quando i vecchi utenti effettuano l'accesso utilizzando l'algoritmo di appartenenza SQL mentre utilizzano l'algoritmo di crittografia in Identità per i nuovi utenti.

La classe UserManager ha una proprietà 'PasswordHasher' che memorizza un'istanza di una classe che implementa l'interfaccia 'IPasswordHasher'. Viene utilizzato per crittografare/decrittografare le password durante le transazioni di autenticazione dell'utente. Nella classe UserManager definita nel passaggio 3, creare una nuova classe SQLPasswordHasher e copiare il codice seguente.

quindi bisogna creare nuova classe hasher con il codice folowing:

public class SQLPasswordHasher : PasswordHasher 
    { 
    public override string HashPassword(string password) 
    { 
     return base.HashPassword(password); 
    } 

public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) 
    { 
     string[] passwordProperties = hashedPassword.Split('|'); 
     if (passwordProperties.Length != 3) 
     { 
      return base.VerifyHashedPassword(hashedPassword, providedPassword); 
     } 
     else 
     { 
      string passwordHash = passwordProperties[0]; 
      int passwordformat = 1; 
      string salt = passwordProperties[2]; 
      if (String.Equals(EncryptPassword(providedPassword, passwordformat, salt), passwordHash, StringComparison.CurrentCultureIgnoreCase)) 
      { 
       return PasswordVerificationResult.SuccessRehashNeeded; 
      } 
      else 
      { 
       return PasswordVerificationResult.Failed; 
      } 
     } 
    } 

//This is copied from the existing SQL providers and is provided only for back-compat. 
    private string EncryptPassword(string pass, int passwordFormat, string salt) 
    { 
     if (passwordFormat == 0) // MembershipPasswordFormat.Clear 
      return pass; 

     byte[] bIn = Encoding.Unicode.GetBytes(pass); 
     byte[] bSalt = Convert.FromBase64String(salt); 
     byte[] bRet = null; 

     if (passwordFormat == 1) 
     { // MembershipPasswordFormat.Hashed 
      HashAlgorithm hm = HashAlgorithm.Create("SHA1"); 
      if (hm is KeyedHashAlgorithm) 
      { 
       KeyedHashAlgorithm kha = (KeyedHashAlgorithm)hm; 
       if (kha.Key.Length == bSalt.Length) 
       { 
        kha.Key = bSalt; 
       } 
       else if (kha.Key.Length < bSalt.Length) 
       { 
        byte[] bKey = new byte[kha.Key.Length]; 
        Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length); 
        kha.Key = bKey; 
       } 
       else 
       { 
        byte[] bKey = new byte[kha.Key.Length]; 
        for (int iter = 0; iter < bKey.Length;) 
        { 
         int len = Math.Min(bSalt.Length, bKey.Length - iter); 
         Buffer.BlockCopy(bSalt, 0, bKey, iter, len); 
         iter += len; 
        } 
        kha.Key = bKey; 
       } 
       bRet = kha.ComputeHash(bIn); 
      } 
      else 
      { 
       byte[] bAll = new byte[bSalt.Length + bIn.Length]; 
       Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length); 
       Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length); 
       bRet = hm.ComputeHash(bAll); 
      } 
     } 

     return Convert.ToBase64String(bRet); 
    } 

quindi dichiarare nella classe Identity UserManager un contructor di utilizzare questo Hasher, ad esempio:

public UserManager() 
     : base(new UserStore<User>(new ApplicationDbContext())) 
    { 
     this.PasswordHasher = new SQLPasswordHasher(); 
} 
0

È indicato nel seguente link: http://kevin-junghans.blogspot.com/2014/02/migrating-existing-website-from.html

public class SimplePasswordHasher : IPasswordHasher 
    { 
    public string HashPassword(string password) 
    { 
     return Crypto.HashPassword(password); 
    } 
    public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) 
    { 
     if(Crypto.VerifyHashedPassword(hashedPassword, providedPassword)) 
     return PasswordVerificationResult.Success; 
     else return PasswordVerificationResult.Failed; 
     } 
    }