users tutorial net mvc manage framework example con autenticaciĆ³n asp and c# asp.net asp.net-mvc asp.net-mvc-4 forms-authentication

c# - tutorial - mvc 5 identity login



MigraciĆ³n de usuarios existentes de MVC 4 SimpleMembership a MVC 5 ASP.NET Identity (2)

De acuerdo a asp.net/identity/overview/migrations/… :

Las contraseñas de los usuarios de la aplicación están cifradas y almacenadas en la base de datos. El algoritmo de cifrado utilizado en la pertenencia a SQL es diferente del que se encuentra en el nuevo sistema de Identidad. Para reutilizar contraseñas antiguas, necesitamos descifrarlas de manera selectiva cuando los usuarios antiguos inicien sesión utilizando el algoritmo de membresía de SQL mientras usamos el algoritmo de cifrado en Identidad para los nuevos usuarios.

La clase UserManager tiene una propiedad ''PasswordHasher'' que almacena una instancia de una clase que implementa la interfaz ''IPasswordHasher''. Esto se utiliza para cifrar / descifrar contraseñas durante las transacciones de autenticación de usuario. En la clase UserManager definida en el paso 3, cree una nueva clase SQLPasswordHasher y copie el siguiente código.

Así que tienes que crear una nueva clase hasher con el siguiente código:

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); }

Luego declare en su clase Identity UserManager un contructor para usar este hasher, por ejemplo:

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

Tengo un sitio MVC 4 que actualmente implementa SimpleMembership . En la siguiente iteración del sitio, me gustaría actualizar a MVC 5 y ASP.NET Identity . Ambos sitios tienen la misma clave de máquina en web.config. Las tablas SQL de SimpleMembership tienen una columna para Contraseña y PaswordSalt las tablas de Identidad ASP.NET tienen una columna para PasswordHash que parece ser una combinación de Contraseña + Contraseña .

Intenté concatenar la contraseña y el PasswordSlat juntos desde el sitio anterior, pero esto no funciona.

Mi pregunta es,

¿Cómo migro las contraseñas de mis usuarios existentes del sitio antiguo al sitio nuevo?


Se indica en el siguiente enlace: here

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; } }