Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在使用ASP.NET身份确认帐户后执行自动登录_C#_Asp.net_Asp.net Mvc_Email_Asp.net Identity - Fatal编程技术网

C# 如何在使用ASP.NET身份确认帐户后执行自动登录

C# 如何在使用ASP.NET身份确认帐户后执行自动登录,c#,asp.net,asp.net-mvc,email,asp.net-identity,C#,Asp.net,Asp.net Mvc,Email,Asp.net Identity,我目前正在开发一个MVC5 web应用程序,使用ASP.NET Identity 2进行帐户控制,具体问题是在确认电子邮件后自动登录用户 因此,流程如下所示: 用户点击注册链接 输入电子邮件(用户名)/密码,点击注册表 将向给定地址发送一封带有“请确认帐户”URL的电子邮件 用户单击链接,确认电子邮件地址并自动登录 控制器上的确认电子邮件操作如下所示: [AllowAnonymous] public async Task<ActionResult> ConfirmEmail(stri

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

因此,流程如下所示:

  • 用户点击注册链接
  • 输入电子邮件(用户名)/密码,点击注册表
  • 将向给定地址发送一封带有“请确认帐户”URL的电子邮件
  • 用户单击链接,确认电子邮件地址并自动登录
  • 控制器上的确认电子邮件操作如下所示:

    [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");
    }
    
    这是非常令人难以置信的,因为我知道db和应用服务器不是问题所在

    我是不是走错方向了


    提前谢谢

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

    async Task IRunBeforeEachRequest.Execute()
    {
      _HttpContext.Items[IdentityContextTransactionKey] = _IdentityContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
      _HttpContext.Items[TravellersContextTransactionKey] = _TravellersContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
    }
    

    再次感谢你的帮助

    是否有可用的或您可以制作的
    FirstOrDefaultAsync
    ?这意味着如果我理解正确,它正在等待
    user
    ,并且
    user
    当前通过同步代码分配。可能存在这样一个问题:您正在对同步和异步方法所使用的相同基础
    DbContext
    进行变异?只是个哑巴gues@tacos_tacos_tacos:是的,你觉得这会有所不同吗?我想我会试试。。。不管怎么说,幽默我,试试看。别忘了,您需要为succeeded@tacos_tacos_tacos:没有区别,相同的错误:
    System.ComponentModel.Win32Exception:wait操作超时
    ,在此基础调用上失败:
    var userIdentity=wait manager.createidentitysync(此为DefaultAuthenticationTypes.applicationcokie)
    
    async Task IRunBeforeEachRequest.Execute()
    {
      _HttpContext.Items[IdentityContextTransactionKey] = _IdentityContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
      _HttpContext.Items[TravellersContextTransactionKey] = _TravellersContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);
    }