【发布时间】:2021-12-14 12:43:48
【问题描述】:
我正在使用 ASP.NET Core Identity 在使用 Dapper ORM 的自定义数据访问层之上构建一个 ASP.NET Core 5 Web API。从根本上说,事情按预期工作,但我意识到身份框架提供的密码验证规则没有任何效果,我不明白发生了什么。这是我所拥有的:
首先,因为我依赖于自定义数据访问层,所以我提供了 Identity 的 IUserStore 接口的自定义实现。
public class UserStore : IUserStore<AppUser>,
IUserPasswordStore<AppUser>,
IUserEmailStore<AppUser>
{
private IRepository<AppUser> _repository;
public UserStore(IConfiguration configuration)
{
_repository = new AppUserRepository(configuration.GetConnectionString("MyConnectionString"));
}
// IUserStore implementation
// IUserPasswordStore implementation
// IUserEmailStore implementation
}
接下来,有一个绑定模型,用于提交创建新帐户所需的信息。
public class RegisterBindingModel
{
[Required]
[Display(Name = "UserName")]
public string UserName
{
get;
set;
}
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password
{
get;
set;
}
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare(nameof(Password), ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword
{
get;
set;
}
// remaining required properties
}
接下来,通过AccountController 创建新帐户:
[Authorize]
[ApiController]
[Route("api/Accounts")]
public class AccountController
{
private readonly UserManager<AppUser> _userManager;
private readonly IPasswordHasher<AppUser> _passwordHasher;
public AccountController(UserManager<AppUser> userManager, IPasswordHasher<AppUser> passwordHasher)
{
_userManager = userManager;
_passwordHasher = passwordHasher;
}
[AllowAnonymous]
[HttpPost]
[Route("Register")]
public async Task<ActionResult> Register([FromBody]RegisterBindingModel model)
{
if(model == null)
{
return BadRequest();
}
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = new AppUser()
{
UserName = model.UserName,
Firstname = model.Firstname,
Lastname = model.Lastname,
Email = model.Email,
Gender = model.Gender
};
user.PasswordHash = _passwordHasher.HashPassword(user, model.Password);
IdentityResult result = await _userManager.CreateAsync(user);
return GetHttpResponse(result);
}
[AllowAnonymous]
[HttpPost]
[Route("Token")]
public async Task<IActionResult> Login([FromForm]LoginBindingModel model)
{
if(model == null)
{
return BadRequest();
}
if(!ModelState.IsValid)
{
return BadRequest(ModelState);
}
AppUser user = await _userManager.FindByNameAsync(model.UserName);
if(user == null || !await _userManager.CheckPasswordAsync(user, model.Password))
{
return Unauthorized();
}
DateTime currentTime = DateTime.UtcNow;
JwtSecurityTokenHandler jwtTokenHandler = new();
SecurityTokenDescriptor tokenDescriptor = new()
{
Subject = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, user.AppUserId.ToString()) }),
IssuedAt = currentTime,
Expires = currentTime.AddHours(_accessTokenValidityInHours),
SigningCredentials = _signingCredentialsProvider.GetSigningCredentials()
};
return Ok(jwtTokenHandler.WriteToken(jwtTokenHandler.CreateToken(tokenDescriptor)));
}
...
}
最后,事情是这样连接在一起的:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration
{
get;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityCore<AppUser>(options => Configuration.GetSection(nameof(IdentityOptions)).Bind(options));
services.AddScoped<IPasswordHasher<AppUser>, Identity.PasswordHasher<AppUser>>();
services.AddTransient<IUserStore<AppUser>, UserStore>();
...
}
}
对应的设置保存在appsettings.json文件中:
{
"IdentityOptions": {
"Password": {
"RequiredLength": 6,
"RequiredUniqueChars": 6,
"RequireNonAlphanumeric": true,
"RequireUppercase": true,
"RequireLowercase": true,
"RequireDigit": true
},
"Lockout": {
"AllowedForNewUsers": true,
"MaxFailedAccessAttempts ": 5,
"DefaultLockoutTimeSpan ": "00:05:00"
}
},
...
}
如果我发送带有必要帐户数据的 HTTP POST 请求,那么密码是什么实际上并不重要。即使我只是将1 作为明显违反密码规则的密码,调用也会成功。 if(!ModelState.IsValid) 的声明很高兴地告诉我,该模型一切正常。
据我所知,ASP.NET Core Identity 提供了一个PasswordValidator,显然应该根据提供的设置来验证密码。从我得到的结果来看,该验证器没有在我的设置中运行。
我不清楚事情是否应该按原样工作,或者我是否需要实施一些我不知道的事情。有没有人有更多的见解,可以告诉我我在这里缺少什么?
编辑:
我刚刚意识到默认的UserManager 公开了IPasswordValidator 对象的列表。我是否使用该列表来验证AccountController 的Register 方法中的密码?
【问题讨论】:
-
您的登录方法在哪里?您应该在某处调用 _passwordHasher.VerifyPassword 进行验证(在 Authenticator 中)。示例之一 - jasonwatmore.com/post/2019/10/21/…
-
作为参考,我添加了登录方法。但是,由于密码是在创建帐户时定义的,所以我看不到与 Login 方法的连接。我的问题是创建帐户时未验证密码。
Identity提供了一个密码验证器,在我的情况下似乎没有运行。
标签: c# .net asp.net-core asp.net-identity