2017-08-26 78 views
0

我想爲新用戶設置確認電子郵件。Asp net核心ConfirmEmail不起作用

[HttpGet] 
    [AllowAnonymous] 
    public async Task<IActionResult> ConfirmEmail(string userId, string code) 
    { 
     if (userId == null || code == null) 
     { 
      return RedirectToAction(nameof(HomeController.Index), "Home"); 
     } 
     var user = await _userManager.FindByIdAsync(userId); 
     if (user == null) 
     { 
      throw new ApplicationException($"Unable to load user with ID '{userId}'."); 
     } 
     var result = await _userManager.ConfirmEmailAsync(user, code); 
     return View(result.Succeeded ? "ConfirmEmail" : "Error"); 
    } 

所以調試時鏈接被點擊時的用戶標識確定,但「碼」的輸入參數爲空,因此

if (userId == null || code == null) 

是真實的,然後它執行

return RedirectToAction(nameof(HomeController.Index), "Home"); 

的發送給註冊用戶的電子郵件是這樣的:

請點擊確認您的帳戶荷蘭國際集團此鏈接:https://開頭本地主機:44314 /帳戶/ ConfirmEmail用戶id = 3ec7ac6a-3329-4821-a09b-aa4843598eaa &代碼= CfDJ8JouO%2BAfPaZIsebmFKKodcE1jEFscFSMcDTvnUPw88tqAKIh0%2BFV6X%2BWCF6fRBgprsymV37RsZsupPoRwCoj8tTT8CckBr0BP9se6DuBxd%2B8fDg2go2S0X9o%2FD9outoU7ShVJl3r3lM5yMXjevtJBoQha9g66ithx%2BhM4Dfskpzt79Imyad6BC0s8s53C7qGZhIx5Dh6DU2KXcVues8XxYQAAhFvzn%2BT49N3ze1%2BihB4Ciwxo5En6sT%2BmbaWvX9N2A% 3D%3D「>鏈接

編輯:鏈接包含&放大器;代碼,而不是&代碼 但doesen't顯示堆棧溢出

從字面上看沒有人有互聯網,所以我在同一個問題」米在這裏輸了。 我在做什麼錯?

public static class UrlHelperExtensions 
{ 
    public static string EmailConfirmationLink(this IUrlHelper urlHelper, string userId, string code, string scheme) 
    { 
     return urlHelper.Action(
      action: nameof(AccountController.ConfirmEmail), 
      controller: "Account", 
      values: new { userId, code }, 
      protocol: scheme); 
    } 

    public static string ResetPasswordCallbackLink(this IUrlHelper urlHelper, string userId, string code, string scheme) 
    { 
     return urlHelper.Action(
      action: nameof(AccountController.ResetPassword), 
      controller: "Account", 
      values: new { userId, code }, 
      protocol: scheme); 
    } 
} 

更新:當我改變&放大器;代碼爲 「&代碼」 中的鏈接,並粘貼它在Chrome它的工作

+0

這裏有一個問題,它還沒有確認,如果它是一個錯誤,但可能是項目模板中的錯誤,電子郵件確認仍然適用於我後更新到2.0,但我的代碼是有點不同於標準項目模板https ://github.com/aspnet/Home/issues/2152#issuecomment-324342230 –

+0

你能告訴你如何填寫'code'嗎?你是否發送這行'字符串代碼=等待UserManager.GenerateEmailConfirmationTokenAsync(user.Id);'作爲'code'參數的值到'ConfirmEmail'? –

+0

var user = new ApplicationUser {UserName = model.Email,Email = model.Email}; var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); – Jones

回答

1
public static Task SendEmailConfirmationAsync(this IEmailSender emailSender, string email, string link) 
    { 
     return emailSender.SendEmailAsync(email, "Confirm your email", 
      $"Please confirm your account by clicking this link: <a href='{HtmlEncoder.Default.Encode(link)}'>link</a>"); 
    } 

的問題是在這裏

HtmlEncoder.Default.Encode(link) 

剛刪除它,它的作品

<a href='{link}'>link</a>