如何在使用ASP.NET标识确认帐户后执行自动登录

时间:2015-06-24 06:15:04

标签: c# asp.net asp.net-mvc email asp.net-identity

我目前正在开发一个MVC5网络应用程序,使用ASP.NET Identity 2进行帐户控制,我遇到的具体问题是在确认电子邮件确认后自动登录用户。 / p>

所以流程如下:

  1. 用户点击注册链接
  2. 输入电子邮件(用户名)/密码,点击注册
  3. 电子邮件将通过“please confirmaccount”URL
  4. 发送到指定地址
  5. 用户点击链接,确认电子邮件地址并自动登录
  6. 控制器上的确认电子邮件操作如下所示:

    [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, false);
    

    这是非常令人难以置信的,因为我知道db和app服务器不是问题。

    我是以错误的方式解决这个问题吗?

    提前致谢!

1 个答案:

答案 0 :(得分:2)

好的,已找到答案,问题如下,对于每个请求,正在设置级别equals()的事务。这导致第二次更改(创建)被第一次更改(电子邮件确认),导致超时...

Read Commited

再次感谢您的帮助!