ASP.NET 核心身份 - 创建用户 "manually" 并提供密码哈希 - 如何正确生成哈希?
ASP.NET Core Identity - Creating user "manually" and providing password hash - How to generate hash correctly?
我必须手动创建用户:
var user = new User
{
Name = "Joe",
Email = "test@****.com",
PasswordHash = "gdfgdfgre2132143xcxzvb=="
}
context.Users.Add(user);
但问题是,当我尝试登录该帐户时,我的密码不起作用。
我将我知道密码的用户帐户的密码哈希复制到,然后我将它粘贴到那个新用户并尝试使用相同的密码但它没有用 - password sign in failed
所以我想问 - 我的逻辑有什么问题,我怎样才能让它发挥作用?
是否与 SecurityStamp 有关?
如果要手动添加用户,还应该设置NormalizedUserName
属性。此外,最好使用 IPasswordHasher<TUser> Interface
来散列密码:
注入的服务:
private readonly ApplicationDbContext _context;
public readonly IPasswordHasher<IdentityUser> _passwordHasher;
public HomeController( ApplicationDbContext dbContext, IPasswordHasher<IdentityUser> _passwordHasher)
{
this._context = dbContext;
this._passwordHasher = _passwordHasher;
}
我假设你的 User
继承了 IdentityUser
,这里我使用 IdentityUser
例如:
IdentityUser applicationUser = new IdentityUser();
Guid guid = Guid.NewGuid();
applicationUser.Id = guid.ToString();
applicationUser.UserName = "Joe";
applicationUser.Email = "wx@hotmail.com";
applicationUser.NormalizedUserName = "wx@hotmail.com";
_context.Users.Add(applicationUser);
var hashedPassword = _passwordHasher.HashPassword(applicationUser, "YourPassword");
applicationUser.SecurityStamp = Guid.NewGuid().ToString();
applicationUser.PasswordHash = hashedPassword;
_context.SaveChanges();
您还可以使用 UserManager.CreateAsync
使用给定密码在后备存储中创建指定用户:
var user = new IdentityUser { UserName = "Joe", Email = "wx@hotmail.com" };
var result = await _userManager.CreateAsync(user, "YourPassWord");
if (result.Succeeded)
{
}
注意:您应该在登录时提供 Email
值。
我必须手动创建用户:
var user = new User
{
Name = "Joe",
Email = "test@****.com",
PasswordHash = "gdfgdfgre2132143xcxzvb=="
}
context.Users.Add(user);
但问题是,当我尝试登录该帐户时,我的密码不起作用。
我将我知道密码的用户帐户的密码哈希复制到,然后我将它粘贴到那个新用户并尝试使用相同的密码但它没有用 - password sign in failed
所以我想问 - 我的逻辑有什么问题,我怎样才能让它发挥作用?
是否与 SecurityStamp 有关?
如果要手动添加用户,还应该设置NormalizedUserName
属性。此外,最好使用 IPasswordHasher<TUser> Interface
来散列密码:
注入的服务:
private readonly ApplicationDbContext _context;
public readonly IPasswordHasher<IdentityUser> _passwordHasher;
public HomeController( ApplicationDbContext dbContext, IPasswordHasher<IdentityUser> _passwordHasher)
{
this._context = dbContext;
this._passwordHasher = _passwordHasher;
}
我假设你的 User
继承了 IdentityUser
,这里我使用 IdentityUser
例如:
IdentityUser applicationUser = new IdentityUser();
Guid guid = Guid.NewGuid();
applicationUser.Id = guid.ToString();
applicationUser.UserName = "Joe";
applicationUser.Email = "wx@hotmail.com";
applicationUser.NormalizedUserName = "wx@hotmail.com";
_context.Users.Add(applicationUser);
var hashedPassword = _passwordHasher.HashPassword(applicationUser, "YourPassword");
applicationUser.SecurityStamp = Guid.NewGuid().ToString();
applicationUser.PasswordHash = hashedPassword;
_context.SaveChanges();
您还可以使用 UserManager.CreateAsync
使用给定密码在后备存储中创建指定用户:
var user = new IdentityUser { UserName = "Joe", Email = "wx@hotmail.com" };
var result = await _userManager.CreateAsync(user, "YourPassWord");
if (result.Succeeded)
{
}
注意:您应该在登录时提供 Email
值。