Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform an automatic login after confirming the account with ASP.NET Identity

I'm currently developing an MVC5 web app, using ASP.NET Identity 2 for account control, the specific problem I'm having concerns automatically logging in the user after e-mail confirmation.

So the flow is as follows:

  1. User clicks on the Register link
  2. Enters email (username) / password, hits Register
  3. An email is sent to the given address with a "please confirmaccount" URL
  4. User clicks the link, confirming email address and is automatically logged in

The confirm email action on the controllers looks like this:

[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 weird thing is, I can either confirm the e-mail or sign in the user, for some reason if I do both... it doesn't work. Specifically, I get a TimeOutException on this line:

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

which is very mind-boggling since I know the db and the app server are not the problem.

Am I going about this the wrong way...?

Thanks in advance!

like image 998
rumblefx0 Avatar asked Jun 24 '15 06:06

rumblefx0


People also ask

How can we get logged in user role in asp net identity?

String[] roles = Roles. GetRolesForUser(); It returns all roles of the currently logged in user.

What is an automatic login?

You can securely save host domain user credentials (Windows logon credentials) by using the auto-login feature. Once enabled, you can automatically log in to your host computer from the same client computer without entering the domain username and password. The feature is enabled by default for Personal and Pro users.


1 Answers

Ok, the answer has been found, problem was the following, for each request a transaction with level Read Commited was being setup. This caused the second change (create) to be blocked by the first (email confirm), causing the 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!

like image 180
rumblefx0 Avatar answered Oct 10 '22 03:10

rumblefx0