我最近更新Asp.Net Identity Core了我的申请表1.0到2.0.
有一些我想尝试的新功能GenerateEmailConfirmationToken,等等.
我正在使用这个项目作为参考.
当用户尝试注册时,我在执行Register方法时遇到错误
private readonly UserManager<ApplicationUser> _userManager;
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var ifUserEXists = _userManager.FindByName(model.EmailId);
if (ifUserEXists == null) return View(model);
var confirmationToken = _userRepository.CreateConfirmationToken();//this is how i'm generating token currently.
var result = _userRepository.CreateUser(model,confirmationToken);
var user = _userManager.FindByName(model.EmailId);
if (result)
{
var code = _userManager.GenerateEmailConfirmationToken(user.Id);//error here
_userRepository.SendEmailConfirmation(model.EmailId, model.FirstName, confirmationToken);
//Information("An email is sent to your email address. Please follow the link to activate your account.");
return …Run Code Online (Sandbox Code Playgroud) 我需要能够通过管理员更改用户的密码.因此,管理员不应输入用户的当前密码,他应该有能力设置新密码.我查看ChangePasswordAsync方法,但此方法需要输入旧密码.因此,此方法不适合此任务.因此我通过以下方式制作:
[HttpPost]
public async Task<ActionResult> ChangePassword(ViewModels.Admin.ChangePasswordViewModel model)
{
var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var result = await userManager.RemovePasswordAsync(model.UserId);
if (result.Succeeded)
{
result = await userManager.AddPasswordAsync(model.UserId, model.Password);
if (result.Succeeded)
{
return RedirectToAction("UserList");
}
else
{
ModelState.AddModelError("", result.Errors.FirstOrDefault());
}
}
else
{
ModelState.AddModelError("", result.Errors.FirstOrDefault());
}
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
它有效,但理论上我们可以在AddPasswordAsync方法上收到错误.因此,旧密码将被删除,但未设置新密码.这不好.有什么方法可以在"一次交易"中做到吗?PS.我看到ResetPasswordAsync方法带有重置令牌,似乎,它更安全(因为不能与用户不稳定的情况)但无论如何,它通过2个动作.
我想知道如果有一种方法与重置密码UserManager的ASP.NET MVC 5
我尝试使用已经有密码但没有成功的用户.任何线索?
IdentityResult result = UserManager.AddPassword(forgotPasswordEvent.UserId.ToString(), model.ConfirmPassword);
if (result.Succeeded)
{
//
}
else
{
AddErrors(result);
}
Run Code Online (Sandbox Code Playgroud) 如何从PasswordHash列在Asp.Net Identity System中以原始形式检索密码?