namespace Microsoft.AspNetCore.Identity { /// /// Provides password hashing and verification methods. /// public static class PasswordHelper { /// /// Hashes a password. /// /// The plain password. /// public static string HashPassword(string plainPassword) { if (string.IsNullOrWhiteSpace(plainPassword)) return plainPassword?.Trim(); var ph = new PasswordHasher(); return ph.HashPassword(null, plainPassword.Trim()); } /// /// Verifies a password with a hashed version. /// /// The plain password. /// The password hash. /// A value indicating whether the password needs a rehash. /// public static bool VerifyPassword(string plainPassword, string hashedPassword, out bool rehashNeeded) { rehashNeeded = false; if (string.IsNullOrWhiteSpace(plainPassword) || string.IsNullOrWhiteSpace(hashedPassword)) return false; var ph = new PasswordHasher(); var result = ph.VerifyHashedPassword(null, hashedPassword, plainPassword); switch (result) { case PasswordVerificationResult.Success: return true; case PasswordVerificationResult.SuccessRehashNeeded: rehashNeeded = true; return true; case PasswordVerificationResult.Failed: default: return false; } } } }