Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check password manually in Asp.Net identity 2?

This might actually be more of a conceptual question. In Asp.Net Identity the PasswordHasher generates a different hash for the same string every time you do:

new PasswordHasher.HashPassword("myString");

Now if for some reason I need to manually compare a user's input to the password saved in the database, I will most probably get a different string when I hash the user's entered password, than the one that is stored in the database.

Can someone please explain this to me? Shouldn't hashing the same string result in the same hash and if not, how does Identity itself realize that two different hashes are in fact the same?

like image 659
Behrooz Avatar asked Dec 23 '15 21:12

Behrooz


People also ask

How does ASP NET identity store passwords?

ASP.NET Core Identity and password hashingThe app will create a hash of the password, and store it in the database along with the user's details. A hash is a one way function, so given the password you can work out the hash, but given the hash you can't get the original password back.

How to reset password in ASP net c#?

After successful login a Change password link will be visible. Here by clicking the link a new page will appear where the user must enter the Current Password, New Password and Confirm Password and then click on the Update button to change his/her password respectively.

What is IdentityDbContext?

IdentityDbContext() Initializes a new instance of the IdentityDbContext class. IdentityDbContext(DbContextOptions) Initializes a new instance of IdentityDbContext.


1 Answers

PasswordHasher generates different hashes each time because it uses salting technique. This technique secure the hashed password against dictionary attacks. By the way you could use following code to manually verify the password:

if(PasswordHasher.VerifyHashedPassword("hashedPassword", "password") 
    != PasswordVerificationResult.Failed)
{
    // password is correct 
}
like image 144
Sam FarajpourGhamari Avatar answered Oct 26 '22 07:10

Sam FarajpourGhamari