I don’t know whether it is possible to handle such a migration through membership without forcing users to go through the reset password process.
But you can do this by moving your Asp.Net Identity membership at the same time: Asp.Net Identity has extension points that allow you to process a “spare” password signature to support the old one. At this point, you still have a login password and a password that you can lose in memory, and then you can convert the signature to a new format.
, , SQL, .
:
public class BackCompatPasswordHasher : 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;
}
}
}
private string EncryptPassword(string pass, int passwordFormat,
string salt)
{
if (passwordFormat == 0)
return pass;
byte[] bIn = Encoding.Unicode.GetBytes(pass);
byte[] bSalt = Convert.FromBase64String(salt);
byte[] bRet = null;
if (passwordFormat == 1)
{
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);
}
}
:
public class IdentityUserManager : UserManager<IdentityUser>
{
public IdentityUserManager(IUserStore<IdentityUser> store)
: base(store)
{
PasswordHasher = new BackCompatPasswordHasher();
}
}
, , , , . , - , . , IdentityUserManager:
private ConcurrentDictionary<string, string> UserRehashed =
new ConcurrentDictionary<string, string>();
private bool CanRehash(IdentityUser user)
{
return UserRehashed.TryAdd(user.Id, user.Id);
}
protected async override Task<bool> VerifyPasswordAsync(
IUserPasswordStore<IdentityUser, string> store, IdentityUser user,
string password)
{
var hash = await store.GetPasswordHashAsync(user).ConfigureAwait(false);
var verifPassRes = PasswordHasher.VerifyHashedPassword(hash, password);
if (verifPassRes == PasswordVerificationResult.SuccessRehashNeeded &&
CanRehash(user))
{
var chPassRes = await this.ChangePasswordAsync(user.Id,
password, password).ConfigureAwait(false);
if (!chPassRes.Succeeded)
{
}
}
return verifPassRes != PasswordVerificationResult.Failed;
}