Generate a PasswordHash and SecurityStamp

2019-09-06 11:14发布

问题:

I have a Registrations table which new users get put into. A later process creates a database for that user, and inserts an ASP.NET Identity User record from the data in the registration table (email & name).

I'd like to extend this so that at the point of registration, users can enter their password, which will then be set up in the new database.

To do this properly, I'd need to create a SecurityStamp value and then encrypt the password using that to get the PasswordHash, I believe. So then I would store these values in the registrations table, and then I could copy them into the user's new database when that is set up, and they would be able to log in with the password they registered.

How would I do this - generate the SecurityStamp and then hash the password?

回答1:

SecurityStamp can be anything random, non-repeatable - Guid.NewGuid().ToString() does the job nicely.

For password hashing UserManager has property PasswordHasher that does password hashing for you:

var userManager = new UserManager(context);
var passwordHash = userManager.PasswordHasher.HashPassword("mySecurePassword");