C# MVC5中的电子邮件确认
我正在尝试从我的API发送确认电子邮件。邮件发送没有问题 从MVC5加载url时,出现以下错误: 我试过: -->My api和My MVC是托管在两台服务器上的两个项目,因此我尝试使用machineKey validationKey 我使用的代码如下: Web APIC# MVC5中的电子邮件确认,c#,.net,asp.net-mvc,asp.net-mvc-5,asp.net-web-api2,C#,.net,Asp.net Mvc,Asp.net Mvc 5,Asp.net Web Api2,我正在尝试从我的API发送确认电子邮件。邮件发送没有问题 从MVC5加载url时,出现以下错误: 我试过: -->My api和My MVC是托管在两台服务器上的两个项目,因此我尝试使用machineKey validationKey 我使用的代码如下: Web API if (!await db.Users.AnyAsync(c => c.Email == userRequest.Email)) return StatusCode(HttpStatusCode.Not
if (!await db.Users.AnyAsync(c => c.Email == userRequest.Email)) return StatusCode(HttpStatusCode.NotFound);
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
userManager.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>();
var user = await userManager.FindByNameAsync(userRequest.Email);
var userId = user.Id;
var code = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
var url = "MyUrl" + "/Account/ConfirmEmail?userId=" + userId + "&code=" + code;
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
user.ConfirmationToken = code;
UserManager.Update(user);
我认为它抛出“无效令牌”,因为代码参数对于querystring来说太复杂(包含特殊字符)。因此,它没有正确地重定向到页面。要解决这个问题:
string val = HttpServerUtility.UrlTokenEncode(Encoding.ASCII.GetBytes(code));
您可以使用此选项更改为“代码”参数 要使令牌确认生效,需要将令牌保存在用户表
AspNetUsers
中
Web API
if (!await db.Users.AnyAsync(c => c.Email == userRequest.Email)) return StatusCode(HttpStatusCode.NotFound);
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(userContext));
userManager.UserTokenProvider = new TotpSecurityStampBasedTokenProvider<ApplicationUser, string>();
var user = await userManager.FindByNameAsync(userRequest.Email);
var userId = user.Id;
var code = await userManager.GenerateEmailConfirmationTokenAsync(user.Id);
var url = "MyUrl" + "/Account/ConfirmEmail?userId=" + userId + "&code=" + code;
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
user.ConfirmationToken = code;
UserManager.Update(user);
MVC5
if (userId == null || code == null)
{
return View("Error");
}
var result = await UserManager.ConfirmEmailAsync(userId, code);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
var result = await UserManager.ConfirmEmailAsync(userId, code);
switch (result.Succeeded)
{
case true:
// Your code
case false:
//
default:
//
}
当它不工作时,
code
的值是多少?API和mvc指向同一数据源进行用户管理?@mjwills像这样的userId=e6710368-9dd2-4417-9096-68642b80314f&code=843473@ChetanRanpariya,是的,两个都指向同一个数据库您在mvc中获得了预期的代码和用户ID吗?我尝试复制您的代码并粘贴。。。我也有同样的问题。我不确定,但这个解决方案,但你是否尝试设置网站的机器密钥?所有服务器上的机器密钥是否相同?您可以使用web.config或IIS设置它们