ASP.NET Core错误:实施忘记密码

时间:2017-09-16 00:49:02

标签: c# asp.net-core entity-framework-core asp.net-identity-3

ASP.NET MVC Core 1.1.1 Individual User Accounts模式VS2017 ver 15.3.3 Forgot Password中,我实施了ResetPassWord.cshtml功能。该应用正在使用生成的链接发送电子邮件。我可以打开我的电子邮件,可以看到生成的链接如下。链接正确显示Invalid Token视图。但是当我输入所需的用户名,密码,确认密码信息并单击“提交”按钮时,我收到PasswordReset错误。 问题:如何解决此问题?

注意

一个。如果我已登录并希望将密码重置为其他内容,则AccountCountroller功能正常工作。

B中。不确定它是否相关:我没有使用电子邮件作为用户名。 相反,我已经更改了[Required] [EmailAddress] public string Email { get; set; } 中的电子邮件属性

[Required]
public string UserName { get; set; }

ApplicationUser.cs

等等,并在// Add profile data for application users by adding properties to the ApplicationUser class public class ApplicationUser : IdentityUser { public string UserFullName { get; set; } [Column(TypeName = "varchar(50)")] public string UserEmaill { get; set; } [Column(TypeName = "varchar(15)")] public string UserCity{ get; set; } } 中扩展了用户属性,如下所示:

ApplicationUser.cs

Please reset your password by clicking here: <a href='http://localhost/HPF/Account/ResetPassword?userId=1db73091-8098-4427-9a1a-1897dcabea90&code=CfDJ8IgLyyEqB6dPkvwFmW%2BJlPrg%2Fpz%2FiNTD442K1KoBG7MSx3zp6TFSY5w0Xj49cG87P%2BkGSWalD6YbbRlrLP4qAEORuoaHZM%2BMe0G08Y9EeUycbhNxx%2FpEv2z3sJ%2BMr4ZccLBO08iwlbIxBnXkJ5gu%2Bsnoo5FHAKysp85%2FrLdbS47smj%2Bt9kaaFA5DDBkzmR%2Bb0HTV2FL8CyjH2M1wCjLgcSC2Jg6BIcDAVUd7jEwxB8V9upAVh%2FR3FJCk2OAJU4w4Fg%3D%3D'>link</a>

应用已发送的电子邮件正文

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await _userManager.FindByNameAsync(model.UserName);
        string sUserEmail = user.UserEmail; //I Added this. Note the email is not the user name in my app
        if (user == null)
        {
            // Don't reveal that the user does not exist or is not confirmed
            return View("ForgotPasswordConfirmation");
        }

        // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
        // Send an email with this link
        var code = await _userManager.GeneratePasswordResetTokenAsync(user);
        var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
        await _emailSender.SendEmailAsync(sUserEmail, "Reset Password", $"Please reset your password by clicking here: <a href='{callbackUrl}'>link</a>");
        return View("ForgotPasswordConfirmation");
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ResetPassword(ResetPasswordViewModel model)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }
    var user = await _userManager.FindByNameAsync(model.UserName);
    if (user == null)
    {
        // Don't reveal that the user does not exist
        return RedirectToAction(nameof(AccountController.ResetPasswordConfirmation), "Account");
    }
    var result = await _userManager.ResetPasswordAsync(user, model.Code, model.Password);
    if (result.Succeeded)
    {
        return RedirectToAction(nameof(AccountController.ResetPasswordConfirmation), "Account");
    }
    AddErrors(result);
    return View();
}

控制器

code = System.Net.WebUtility.UrlEncode(code);

更新

  1. 我在var code = await _userManager.GeneratePasswordResetTokenAsync(user); POST方法中的ForgotPassword(...)行之后尝试了var code = System.Net.WebUtility.UrlDecode(model.Code);;但仍然是同样的错误。然后我在var result = await _userManager.ResetPasswordAsync(user, code, model.Password); POST方法中的行ResetPassword(...)之前尝试了var obj = {TestOne: 12, TestTwo: 6, TestThree: 4, TestFour: 2}; // sort the array var arr = Object.entries(obj); arr.sort((a, b) => a[1] - b[1]); // enumerate the array the index would be the rank var arr_with_rank = arr.map((data, index) => [data[0], index+1]).reverse() arr_with_rank.forEach(x => console.log(x[0] + ": " + x[1]));;同样的错误。

0 个答案:

没有答案