我正在开发一个MVC5 Web应用程序,其中成员资格仅限邀请,因此用户帐户必须由管理员预先创建 . 该帐户填充了用户的电子邮件地址,该地址也用作用户名,并且帐户是在没有密码的情况下创建的 .

private async Task CreateAndNotifyUser(string emailAddress)
        {
        var user = new ApplicationUser {UserName = emailAddress, Email = emailAddress};
        var result = await userManager.CreateAsync(user);
        if (!result.Succeeded)
            throw new InvalidOperationException(result.Errors.First());
        await SendNotificationEmail(user.Id);
        }

用户会收到一封通知电子邮件,其中包含一个包含电子邮件验证令牌的链接 . 令牌生成如下:

private async Task SendNotificationEmail(string userId)
        {
        var code = await userManager.GenerateEmailConfirmationTokenAsync(userId);
        var emailModel = new VerificationTokenEmailModel
            {
            ApplicationName = "MVC5 Application",
            CallbackUrl = Url.Action("ConfirmEmail", "UserAdministration", new {userId, code}, Request.Url.Scheme),
            InformationUrl = Url.Action("Index", "Home"),
            VerificationToken = code
            };
        var emailBody = razor.RunCompile("NewUserInvitation.cshtml", typeof(VerificationTokenEmailModel), emailModel);
        await userManager.SendEmailAsync(userId, "Invitation", emailBody);
        }

当用户打开他们的电子邮件并点击链接时,我通过UserManager将他们的电子邮件标记为“已确认”,然后我为用户生成密码重置令牌,并重定向到AccountController中的ResetPassword操作,如下所示:

public async Task<ActionResult> ConfirmEmail(string userId, string code)
        {
        if (userId == null || code == null)
            return View("Error");
        var result = await userManager.ConfirmEmailAsync(userId, code);
        if (!result.Succeeded)
            throw new InvalidOperationException(result.Errors.FirstOrDefault());
        var token = await userManager.GeneratePasswordResetTokenAsync(userId);
        code = HttpUtility.UrlEncode(token);
        return RedirectToAction("ResetPassword", "Account", new {code});
        }

但是,当用户输入其电子邮件和两个匹配的密码时,他们会在验证摘要中收到错误“无效令牌” .

我在其他问题中读到这可能是令牌的HTTP编码的问题,但是我已经检查了生成的令牌,它们似乎不包含或空格字符 . 无论有没有这条线,我都试过了:

code = HttpUtility.UrlEncode(token);

我已经在这两天了,我不知道为什么会这样 . 我更加疑惑为什么电子邮件验证令牌工作得很好,但密码重置令牌却没有 . 任何想法,任何人?

更新时间:2016-07-18

我尝试使用正常的 Register() 操作注册用户,并按照确认电子邮件地址的顺序进行操作 . 如果此时我手动激活 /Account/ForgotPassword 操作,则密码重置有效 . 所以显然有一些不同之处在于我没有看到它的外观,它可能是简单但不明显的东西 .