dan*_*iel 88 asp.net asp.net-identity
如何在新的ASP.NET身份系统中获取用户的密码?或者如何在不知道当前密码(用户忘记密码)的情况下重置?
小智 127
或者如何在不知道当前密码(用户忘记密码)的情况下重置?
如果要使用UserManager更改密码但不想提供用户的当前密码,则可以生成密码重置令牌,然后立即使用它.
string resetToken = await UserManager.GeneratePasswordResetTokenAsync(model.Id);
IdentityResult passwordChangeResult = await UserManager.ResetPasswordAsync(model.Id, resetToken, model.NewPassword);
Run Code Online (Sandbox Code Playgroud)
jd4*_*d4u 98
在当前版本中
假设您已经处理了重置忘记密码的请求的验证,请使用以下代码作为示例代码步骤.
ApplicationDbContext =new ApplicationDbContext()
String userId = "<YourLogicAssignsRequestedUserId>";
String newPassword = "<PasswordAsTypedByUser>";
ApplicationUser cUser = UserManager.FindById(userId);
String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();
store.SetPasswordHashAsync(cUser, hashedNewPassword);
Run Code Online (Sandbox Code Playgroud)
在AspNet Nightly Build中
更新框架以与Token一起处理ForgetPassword等请求.一旦发布,预计会提供简单的代码指导.
更新:
此更新仅用于提供更明确的步骤.
ApplicationDbContext context = new ApplicationDbContext();
UserStore<ApplicationUser> store = new UserStore<ApplicationUser>(context);
UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(store);
String userId = User.Identity.GetUserId();//"<YourLogicAssignsRequestedUserId>";
String newPassword = "test@123"; //"<PasswordAsTypedByUser>";
String hashedNewPassword = UserManager.PasswordHasher.HashPassword(newPassword);
ApplicationUser cUser = await store.FindByIdAsync(userId);
await store.SetPasswordHashAsync(cUser, hashedNewPassword);
await store.UpdateAsync(cUser);
Run Code Online (Sandbox Code Playgroud)
Sha*_*tin 70
这是最初的答案.它确实有效,但有问题.如果AddPassword
失败怎么办?用户没有密码.
原答案:我们可以使用三行代码:
UserManager<IdentityUser> userManager =
new UserManager<IdentityUser>(new UserStore<IdentityUser>());
userManager.RemovePassword(userId);
userManager.AddPassword(userId, newPassword);
Run Code Online (Sandbox Code Playgroud)
另见:http://msdn.microsoft.com/en-us/library/dn457095(v = vs111).aspx
最好使用EdwardBrey提出的答案,然后DanielWright用代码示例进行详细阐述.
Edw*_*rey 28
在你的UserManager
,首先调用GeneratePasswordResetTokenAsync.一旦用户验证了他的身份(例如通过在电子邮件中接收令牌),将令牌传递给ResetPasswordAsync.
在用于 Web API 的 Asp.Net Core Identity 中重置密码的最佳方法。
注意* :Error() 和 Result() 是为内部使用而创建的。你可以返回你想要的。
[HttpPost]
[Route("reset-password")]
public async Task<IActionResult> ResetPassword(ResetPasswordModel model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
try
{
if (model is null)
return Error("No data found!");
var user = await _userManager.FindByIdAsync(AppCommon.ToString(GetUserId()));
if (user == null)
return Error("No user found!");
Microsoft.AspNetCore.Identity.SignInResult checkOldPassword =
await _signInManager.PasswordSignInAsync(user.UserName, model.OldPassword, false, false);
if (!checkOldPassword.Succeeded)
return Error("Old password does not matched.");
string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user);
if (string.IsNullOrEmpty(resetToken))
return Error("Error while generating reset token.");
var result = await _userManager.ResetPasswordAsync(user, resetToken, model.Password);
if (result.Succeeded)
return Result();
else
return Error();
}
catch (Exception ex)
{
return Error(ex);
}
}
Run Code Online (Sandbox Code Playgroud)