我想为新用户设置确认电子邮件。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它的工作
这里有一个问题,它还没有确认,如果它是一个错误,但可能是项目模板中的错误,电子邮件确认仍然适用于我后更新到2.0,但我的代码是有点不同于标准项目模板https ://github.com/aspnet/Home/issues/2152#issuecomment-324342230 –
你能告诉你如何填写'code'吗?你是否发送这行'字符串代码=等待UserManager.GenerateEmailConfirmationTokenAsync(user.Id);'作为'code'参数的值到'ConfirmEmail'? –
var user = new ApplicationUser {UserName = model.Email,Email = model.Email}; var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); – Jones