【发布时间】:2018-07-31 21:05:21
【问题描述】:
我正在尝试从我的 API 发送确认电子邮件。邮件发送没有问题。
当我从 MVC5 加载 url 时,出现以下错误:
我试过了:
Asp.NET Identity 2 giving "Invalid Token" error
http://www.gunaatita.com/Blog/Invalid-Token-Error-on-Email-Confirmation-in-Aspnet-Identity/1056 --> 我的 api 和我的 MVC 是托管在两台服务器上的两个项目,因此我尝试使用 machineKey 验证密钥。
我使用的代码如下:
网络 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;
MVC5
if (userId == null || code == null)
{
return View("Error");
}
var result = await UserManager.ConfirmEmailAsync(userId, code);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
【问题讨论】:
-
code不起作用时的值是多少? -
API和mvc指向同一个数据源进行用户管理?
-
@mjwills 喜欢这个 userId=e6710368-9dd2-4417-9096-68642b80314f&code=843473
-
@ChetanRanpariya,是的,它们都指向同一个数据库
-
你在mvc中得到预期的code和userId了吗?
标签: c# .net asp.net-mvc asp.net-mvc-5 asp.net-web-api2