Skip to content

Instantly share code, notes, and snippets.

@svenrog
Last active January 31, 2022 12:48
Show Gist options
  • Save svenrog/c14461901ee5136a20f3df56af664466 to your computer and use it in GitHub Desktop.
Save svenrog/c14461901ee5136a20f3df56af664466 to your computer and use it in GitHub Desktop.
Hasher that will fall back to legacy hashing.
using Microsoft.AspNetCore.Identity;
namespace YourProject.Infrastructure.Security
{
public class FallbackPasswordHasher<TUser> : PasswordHasher<TUser>
where TUser : class
{
public override PasswordVerificationResult VerifyHashedPassword(TUser user, string hashedPassword, string providedPassword)
{
var result = base.VerifyHashedPassword(user, hashedPassword, providedPassword);
if (result == PasswordVerificationResult.Success)
return result;
if (LegacyAspNetIdentityCrypto.VerifyHashedPassword(hashedPassword, providedPassword))
// NOTE: You can either accept the hash as is, like
// return PasswordVerificationResult.Success;
// or return that a rehash is needed (to update the hash),
// this will look like the first attempt is a fail, the second attempt will be a success
return PasswordVerificationResult.SuccessRehashNeeded;
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment