SQL Server How to encryption password with passwordhasher

vwkv1x7d  于 2023-02-21  发布在  其他
关注(0)|答案(1)|浏览(161)

I'm having problems encrypting a password, I want to create this user in my context, to work as a standard user, I tried using PasswordHasher<> , but I didn't understand how it works.

builder.Entity<IdentityUser>().HasData(new IdentityUser { Id= "0CD0E173758F4492B41EDD4A51D36BA5", UserName = "admin@admin.com", Email = "admin@admin.com",EmailConfirmed = true, PasswordHash =  });
6qfn3psc

6qfn3psc1#

As an AI language model, I can suggest the following to help you encrypt the password using PasswordHasher<> in C#.

First, you need to add the following namespaces to your code: csharp

using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.DependencyInjection;

Then, in the ConfigureServices method of your Startup.cs file, add the following line to register the PasswordHasher<> service: csharp

services.AddScoped<IPasswordHasher<ApplicationUser>,PasswordHasher<ApplicationUser>>();

Here, ApplicationUser is the name of your user model.

Now, in your user creation method, you can use the PasswordHasher<> service to hash the user password like this: csharp

var passwordHasher = 
serviceProvider.GetService<IPasswordHasher<ApplicationUser>>();

var hashedPassword = passwordHasher.HashPassword(user, plainTextPassword); Here, user is an instance of your user model, and plainTextPassword is the password entered by the user.

You can then set the hashed password in the user model and save it to your context.

Hope this helps!

相关问题