ASP.NET MVC: Custom Password Hasher with ASP.NET Identity

By FoxLearn 5/29/2017 9:21:44 PM   9.27K
How to Custom encrypt password hasher with ASP.NET Identity MVC 5 using C#, Entity Framework Code First

Step 1: Create an Encrypt class as below

public class Encrypt
{
    public static string GetMD5Hash(string input)
    {
        //Encrypt text using md5
        using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())
        {
            byte[] b = System.Text.Encoding.UTF8.GetBytes(input);
            b = md5.ComputeHash(b);//Hash data
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            foreach (byte x in b)
                sb.Append(x.ToString("x2"));//hexadecimal
            return sb.ToString();
        }
    }
}

Step 2: Create a custom CustomPasswordHasher class as below

public class CustomPasswordHasher : IPasswordHasher
{
    public string HashPassword(string password)
    {
        return Encrypt.GetMD5Hash(password);
    }

    public PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
    {
        if (hashedPassword == HashPassword(providedPassword))
            return PasswordVerificationResult.Success;
        else
            return PasswordVerificationResult.Failed;
    }
}

Step 3: Open IdentityConfig class, then add PasswordHasher as below

public ApplicationUserManager(IUserStore<ApplicationUser> store)
            : base(store)
        {
    PasswordHasher = new CustomPasswordHasher();
}

VIDEO TUTORIALS