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:
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!
String[] roles = Roles. GetRolesForUser(); It returns all roles of the currently logged in user.
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With