rum*_*fx0 8 c# asp.net email asp.net-mvc asp.net-identity
我目前正在开发一个MVC5 Web应用程序,使用ASP.NET Identity 2进行帐户控制,我遇到的具体问题是在电子邮件确认后自动登录用户.
所以流程如下:
控制器上的确认电子邮件操作如下所示:
[AllowAnonymous]
public async Task<ActionResult> ConfirmEmail(string userId, string code)
{
// If userId or code is empty, show an error or redirect
if (userId == null || code == null)
{
return View("Error");
}
// Attempt to confirm the email address
var confirmEmailResult = await UserManager.ConfirmEmailAsync(userId, code);
// Retrieve the user (ApplicationUser)
var user = await UserManager.Users.FirstOrDefaultAsync(u => u.Id == userId);
if (user == null)
{
// If the user doesn't exist, redirect home
return RedirectToAction("Index", "Home");
}
// If the confirmation succeeded, sign in the user and redirect to this profile page
if (confirmEmailResult.Succeeded)
{
await SignInManager.SignInAsync(user, false, false);
var url = Url.Action("Profile", "User");
return RedirectToLocal(url);
}
// In all other cases, redirect to an error page
return View("Error");
}
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我可以确认电子邮件或登录用户,出于某种原因,如果我同时做这两件事......它不起作用.具体来说,我在这一行得到了一个TimeOutException:
await SignInManager.SignInAsync(user, false, false);
Run Code Online (Sandbox Code Playgroud)
这是非常令人难以置信的,因为我知道db和app服务器不是问题.
我是以错误的方式解决这个问题吗?
提前致谢!
好的,答案已经找到,问题如下,对于每个请求,Read Commited正在设置一个具有级别的事务。这导致第二个更改(创建)被第一个更改(电子邮件确认)阻止,从而导致超时......
async Task IRunBeforeEachRequest.Execute()
{
_HttpContext.Items[IdentityContextTransactionKey] = _IdentityContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
_HttpContext.Items[TravellersContextTransactionKey] = _TravellersContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
}
Run Code Online (Sandbox Code Playgroud)
再次感谢您的帮助!
| 归档时间: |
|
| 查看次数: |
2392 次 |
| 最近记录: |