I'm currently migrating the old API that use .Net Framework 4.5.2 to .Net Core 2.1, in the old API that use .Net Framework 4.5.2 there's this script :
PasswordHasher hasher = new PasswordHasher();
password = ConfigurationManager.AppSettings["userDefaultPassword"].ToString();
hashedPassword = hasher.HashPassword(password);
so i want to know, is there any equal function that i can use in .Net Core 2.1 that produce the same hash result as in the old .Net Framework?
I believe that the equivalent is this:
IConfiguration _configuration;
PasswordHasher<User> hasher = new PasswordHasher<User>(
new OptionsWrapper<PasswordHasherOptions>(
new PasswordHasherOptions() {
CompatibilityMode = PasswordHasherCompatibilityMode.IdentityV2
})
);
password = _configuration["userDefaultPassword"].ToString();
hashedPassword = hasher.HashPassword(user, password);
Notes:
IConfiguration
(Configuration
in Startup.cs) rather than ConfigurationManager
.IdentityV2
since it sounds like you want to generate backwards-compatible password hashes (i.e. you can access the database from .NET Framework and understand the hashes generated by .NET Core). If this isn't the case, you can remove it since the verification code can verify older hashes without setting this.OptionsWrapper
is under the Microsoft.Extensions.Options
namespace.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With