c# – 如何在使用ASP.NET标识确认帐户后执行自动登录
发布时间:2020-12-15 07:55:53 所属栏目:百科 来源:网络整理
导读:我目前正在开发一个MVC5 Web应用程序,使用ASP.NET Identity 2进行帐户控制,我遇到的具体问题是在电子邮件确认后自动登录用户. 所以流程如下: 用户单击“注册”链接 输入电子邮件(用户名)/密码,点击注册 通过“please confirmaccount”URL将电子邮件发送到指
我目前正在开发一个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"); } 奇怪的是,我可以确认电子邮件或登录用户,出于某种原因,如果我同时做这两件事……它不起作用.具体来说,我在这一行得到了一个TimeOutException: await SignInManager.SignInAsync(user,false); 这是非常令人难以置信的,因为我知道db和app服务器不是问题. 我是以错误的方式解决这个问题吗? 提前致谢! 解决方法
好的,已找到答案,问题如下,对于每个请求,正在设置具有级别Read Commited的事务.这导致第二次更改(创建)被第一次更改(电子邮件确认),导致超时…
async Task IRunBeforeEachRequest.Execute() { _HttpContext.Items[IdentityContextTransactionKey] = _IdentityContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); _HttpContext.Items[TravellersContextTransactionKey] = _TravellersContext.Database.BeginTransaction(IsolationLevel.ReadCommitted); } 再次感谢您的帮助! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |