How to automatically log in after verifying an account with an ASP.NET ID

I am currently developing a MVC5 web application using ASP.NET Identity 2 for account management, the specific problem I'm having problems is automatically logged in the email confirmation after .

So the stream is as follows:

  • User clicks Register link
  • Included in email (username) / password, hits Registration
  • An email is sent to the indicated address with the URL "please confirmaccount".
  • The user clicks on the link, confirms the email address and automatically registers

Confirmation of the email action on the controllers is as follows:

[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"); } 

The strange thing is that I can either confirm the email, or sign the user, for some reason, if I do both ... it won’t work. In particular, I get a TimeOutException on this line:

 await SignInManager.SignInAsync(user, false, false); 

which is very stunning since I know that db and application server are not a problem.

Am I wrong about this ...?

Thanks in advance!

+6
source share
1 answer

Well, the answer was found, the problem was this: for each request, a transaction with the Read Commited level was configured. This made the second change (create) block first (confirmation by email), causing a timeout ...

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

Thanks again for the help!

+2
source

Source: https://habr.com/ru/post/989654/


All Articles