Asp .net 核心更改密码需要重启应用程序
Asp .net core changing password requires application restart
我正在启动我的应用程序,登录并更改我的密码(我使用默认的 net .core 身份):
IdentityResult identityResult =
await _userManager.ChangePasswordAsync(
applicationUser,
model.CurrentPassword,
model.NewPassword);
这有效,并且在数据库中存储了新的散列密码。
然后,我正在注销并尝试使用新密码登录。但是
if (await _userManager.CheckPasswordAsync(user, password))
returnfalse
。 (使用旧密码登录仍然有效,我没有缓存任何东西)
当我重新启动我的应用程序并尝试使用新密码登录时,它起作用了。
我猜这是 PasswordStore 的某个地方有问题(是否有缓存?)?我可能忘记了什么或为什么这不起作用的任何其他建议?
编辑:
完整修改密码方法:
[HttpPut]
[Route("api/user/changepassword/{ident}")]
public async Task<bool> ChangePassword(int ident, [FromBody]ChangePasswordModel model)
{
if (!ModelState.IsValid)
return false;
ApplicationUser applicationUser;
if ((applicationUser = await _userManager.FindByIdAsync(ident.ToString())) == null)
return false;
IdentityResult identityResult = await _userManager.ChangePasswordAsync(applicationUser, model.CurrentPassword, model.NewPassword);
return identityResult.Succeeded;
}
我的 startup.cs
的一部分
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
所以我猜,AspNetCores UserManager<TUser>
缓存数据(我猜它是由 PasswordStore 缓存的?如果有误,请纠正我。)
我可以通过在令牌提供者中间件中验证密码时获取一个新的 UserManager<TUser>
-object 来修复它。
private async Task _generateToken(HttpContext context)
{
StringValues username = context.Request.Form["username"];
StringValues password = context.Request.Form["password"];
var usermanager = context.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
ApplicationUser user = await usermanager.FindByNameAsync(username);
if (user == null)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid username or password.");
return;
}
ClaimsIdentity identity = await _getIdentity(user, password);
if (identity == null)
{
await usermanager.AccessFailedAsync(user);
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid username or password.");
return;
}
我可以使用以下扩展方法创建一个新的 UserManager<TUser>
:
var usermanager = context.RequestServices.GetRequiredService<UserManager<TUser>>();
验证密码时,我们现在验证新数据,新密码正确(而以前的密码不正确)。
我正在启动我的应用程序,登录并更改我的密码(我使用默认的 net .core 身份):
IdentityResult identityResult =
await _userManager.ChangePasswordAsync(
applicationUser,
model.CurrentPassword,
model.NewPassword);
这有效,并且在数据库中存储了新的散列密码。
然后,我正在注销并尝试使用新密码登录。但是
if (await _userManager.CheckPasswordAsync(user, password))
returnfalse
。 (使用旧密码登录仍然有效,我没有缓存任何东西)
当我重新启动我的应用程序并尝试使用新密码登录时,它起作用了。 我猜这是 PasswordStore 的某个地方有问题(是否有缓存?)?我可能忘记了什么或为什么这不起作用的任何其他建议?
编辑:
完整修改密码方法:
[HttpPut]
[Route("api/user/changepassword/{ident}")]
public async Task<bool> ChangePassword(int ident, [FromBody]ChangePasswordModel model)
{
if (!ModelState.IsValid)
return false;
ApplicationUser applicationUser;
if ((applicationUser = await _userManager.FindByIdAsync(ident.ToString())) == null)
return false;
IdentityResult identityResult = await _userManager.ChangePasswordAsync(applicationUser, model.CurrentPassword, model.NewPassword);
return identityResult.Succeeded;
}
我的 startup.cs
的一部分public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
所以我猜,AspNetCores UserManager<TUser>
缓存数据(我猜它是由 PasswordStore 缓存的?如果有误,请纠正我。)
我可以通过在令牌提供者中间件中验证密码时获取一个新的 UserManager<TUser>
-object 来修复它。
private async Task _generateToken(HttpContext context)
{
StringValues username = context.Request.Form["username"];
StringValues password = context.Request.Form["password"];
var usermanager = context.RequestServices.GetRequiredService<UserManager<ApplicationUser>>();
ApplicationUser user = await usermanager.FindByNameAsync(username);
if (user == null)
{
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid username or password.");
return;
}
ClaimsIdentity identity = await _getIdentity(user, password);
if (identity == null)
{
await usermanager.AccessFailedAsync(user);
context.Response.StatusCode = StatusCodes.Status400BadRequest;
await context.Response.WriteAsync("Invalid username or password.");
return;
}
我可以使用以下扩展方法创建一个新的 UserManager<TUser>
:
var usermanager = context.RequestServices.GetRequiredService<UserManager<TUser>>();
验证密码时,我们现在验证新数据,新密码正确(而以前的密码不正确)。