2017-03-02 49 views
0

我正在嘗試允許我的用戶重置密碼,但我無法弄清楚爲什麼codecallbackUrl返回false。這是我忘記密碼的方法:爲什麼我生成錯誤,沒有註冊IUserTokenProvider?

[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不爲空時會發生這種情況?

+0

將取消投票,直到標題更改爲更具體。 –

+0

可能的副本 http:// stackoverflow。com/questions/22629936/no-iusertokenprovider-is-registered – PAVITRA

回答

0

我有這個完全相同的問題。如果您尚未創建電子郵件服務課程,則需要重新配置您的身份課程。我建議你使用這樣的:

public async Task SendAsync(IdentityMessage message) 
{ 
    MailMessage email = new MailMessage(new 
    MailAddress("[email protected]", "(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="[email protected]"/> 
    <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. 配置Identity類

    public async Task SendAsync(IdentityMessage message) 
    { 
        MailMessage email = new MailMessage(new MailAddress("[email protected]", "(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); 
    
    }
  3. 將您的憑證添加到web.config中。這部分我沒有使用gmail,因爲gmail的使用在我的工作環境中被阻止,並且仍然完美。

    <add key="GmailUserName" value="[email protected]"/> 
    <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. 對帳戶進行控制必要的修改。添加以下突出顯示的代碼。

First do this

Then This

編譯然後運行。乾杯!

+0

歡迎您提供解決方案的鏈接,但請確保您的答案在沒有它的情況下很有用:[在鏈接周圍添加上下文](// meta.stackexchange.com/a/ 8259),所以你的同行用戶會有一些想法是什麼,爲什麼它在那裏,然後引用你鏈接到的頁面中最相關的部分,以防目標頁面不可用。 [答案只是一個鏈接而已,可能會被刪除。](// stackoverflow.com/help/deleted-answers) – NobodyNada

+0

我很接近,從那時起我就工作了很多。我錯過了您在帳戶控制器中指出的'DpapiDataProtectionProvider'位。謝謝! – user6225

+0

@NobodyNada,那更好嗎? – Skullomania

相關問題