为什么我会生成错误,没有注册IUserTokenProvider?

时间:2017-03-02 23:02:11

标签: c# asp.net-mvc sendmail

我正在尝试为我的用户重置密码,但我无法弄清楚为什么codecallbackUrl返回false。这是我忘记密码的方法:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindByNameAsync(model.Email);
        if (user == null || !(await UserManager.IsEmailConfirmedAsync(user.Id)))
        {
            // 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=320771
        // Send an email with this link
        string callbackUrl = await ResetPasswordConfirmationTokenAsync(user.Id, "Password Reset"); 
        return RedirectToAction("ForgotPasswordConfirmation", "Account");
    }

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

see breakpoint info

然后我创建了密码重置任务

private async Task<string> ResetPasswordConfirmationTokenAsync(string userID, string subject)
{
    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
    // Send an email with this link:
    string code = await UserManager.GeneratePasswordResetTokenAsync(userID);
    var callbackUrl = Url.Action("ForgotPassword", "Account", new { userId = userID, code = code }, protocol: Request.Url.Scheme); 
    await UserManager.SendEmailAsync(userID, subject, "Please confirm your account by <a href=\"" + callbackUrl + "\">clicking here</a>");


    return callbackUrl;
}

see breakpoint info for this

由于两个值都返回null,因此会生成错误没有注册IUserTokenProvider。为什么user不为空时会发生这种情况?

1 个答案:

答案 0 :(得分:2)

我有同样的问题。如果您尚未创建电子邮件服务类,则需要重新配置您的身份类。我建议你使用这样的东西:

public async Task SendAsync(IdentityMessage message)
{
    MailMessage email = new MailMessage(new 
    MailAddress("youremailadress@domain.com", "(any subject here)"),
    new MailAddress(message.Destination));
    email.Subject = message.Subject;
    email.Body = message.Body;

    email.IsBodyHtml = true;

    GmailEmailService mailClient = new GmailEmailService();
    await mailClient.SendMailAsync(email);
}

接下来,将您的凭据添加到webconfig

<appSettings>
     <add key="webpages:Version" value="3.0.0.0" />
     <add key="webpages:Enabled" value="false" />
     <add key="ClientValidationEnabled" value="true" />
     <add key="UnobtrusiveJavaScriptEnabled" value="true" />

     <add key="GmailUserName" value="youremail@yourdomain.com"/>
     <add key="GmailPassword" value="yourPassword"/>
     <add key="GmailHost" value="yourServer"/>
     <add key="GmailPort" value="yourPort"/>
     <add key="GmailSsl" value="chooseTrueOrFalse"/>    
</appSettings>

最后,您需要为帐户控制器添加一些更改。请参阅以下内容:

  1. 创建一个名为MyClasses的新文件夹,并创建并添加以下类

    public class GmailEmailService:SmtpClient
    {
        // Gmail user-name
        public string UserName { get; set; }
    
    
    public GmailEmailService() :
        base(ConfigurationManager.AppSettings["GmailHost"], Int32.Parse(ConfigurationManager.AppSettings["GmailPort"]))
    {
        //Get values from web.config file:
        this.UserName = ConfigurationManager.AppSettings["GmailUserName"];
        this.EnableSsl = Boolean.Parse(ConfigurationManager.AppSettings["GmailSsl"]);
        this.UseDefaultCredentials = false;
        this.Credentials = new System.Net.NetworkCredential(this.UserName, ConfigurationManager.AppSettings["GmailPassword"]);
    }
    
    }
  2. 配置您的身份类

    email.IsBodyHtml = true;
    
    GmailEmailService mailClient = new GmailEmailService();
    await mailClient.SendMailAsync(email);
    
  3. 将您的凭据添加到web.config。我没有在这部分使用gmail,因为在我的工作场所阻止了gmail的使用,它仍然可以正常运行。

                       

    public async Task SendAsync(IdentityMessage message)
    {
        MailMessage email = new MailMessage(new MailAddress("youremailadress@domain.com", "(any subject here)"),
        new MailAddress(message.Destination));
        email.Subject = message.Subject;
        email.Body = message.Body;
    
    
    <add key="GmailUserName" value="youremail@yourdomain.com"/>
    <add key="GmailPassword" value="yourPassword"/>
    <add key="GmailHost" value="yourServer"/>
    <add key="GmailPort" value="yourPort"/>
    <add key="GmailSsl" value="chooseTrueOrFalse"/>
    <!--Smptp Server (confirmations emails)-->
    
    }

  4. 对您的帐户管理员进行必要的更改。添加以下突出显示的代码。

  5. First do this

    Then This

    编译然后运行。干杯!