我正在尝试让确认电子邮件验证新的用户帐户。创建令牌并将其通过电子邮件发送给用户,该用户会收到一封电子邮件,其中包含用于验证帐户的链接。当用户单击链接时,我得到了无效的令牌。
它托管在Godaddy上(不确定是否有作用)
在调试代码时,我发现发送给验证的令牌最初与生成的令牌相同,只是现在小写,这可能是问题所在吗?
用于生成令牌并通过电子邮件发送代码的代码
private async Task<string> SendEmailConfirmationTokenAsync(string userID, string subject)
{
string _code = await UserManager.GenerateEmailConfirmationTokenAsync(userID);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userID, code = _code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(userID, subject, "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return callbackUrl;
}
要确认令牌/电子邮件:
[AllowAnonymous]
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)
{
return RedirectToAction("Create", "Users", new { id = userId });
}
AddErrors(result);
ViewBag.errorMessage = "Error: " + result.Errors;
return View("Error");
}
我也将machineKey添加到web.config。
<machineKey validationKey="key" decryptionKey="key" validation="SHA1" decryption="AES" />
我一直收到错误的无效令牌
答案 0 :(得分:0)
对代码进行编码,然后再通过电子邮件发送:
private async Task<string> SendEmailConfirmationTokenAsync(string userID, string subject)
{
string _code = await UserManager.GenerateEmailConfirmationTokenAsync(userID);
_code = HttpUtility.UrlEncode(_code);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = userID, code = _code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(userID, subject, "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return callbackUrl;
}
答案 1 :(得分:0)
这令人难以置信,但是解决方案是创建一个新项目并将所有内容引入其中。
我认为VS创建导致问题的项目时发生了某些事情。
非常感谢