I'm doing an Web Application based on Asp.Net, and the login system based on Identity Framework. The problem that I'm getting is a bit strange.
I run the application that takes me to the login page, I login (and redirected to the homepage) then if I logout and try to login again it keeps giving me the Login page instead the Home page, but if I do this second Login with a BreakPoint in the async call and then go step by step the login is done and I'm redirected to the Home page.
The code is:
[HttpPost]
[AllowAnonymous]
public async Task<ActionResult> LogIn(LogInModel model)
{
if (!ModelState.IsValid)
{
return View();
}
//BreakPoint
var user = await userManager.FindAsync(model.Email, model.Password);
if (user != null)
{
var identity = await userManager.CreateIdentityAsync(
user, DefaultAuthenticationTypes.ApplicationCookie);
SignIn(user);
return Redirect(GetRedirectUrl(model.ReturnUrl));
}
// user authN failed
ModelState.AddModelError("", "Invalid email or password");
return View();
}
private string GetRedirectUrl(string returnUrl)
{
if (string.IsNullOrEmpty(returnUrl) || !Url.IsLocalUrl(returnUrl))
{
return Url.Action("index", "home");
}
return returnUrl;
}
private async Task SignIn(IdentityUser user)
{
var identity = await userManager.CreateIdentityAsync(
user, DefaultAuthenticationTypes.ApplicationCookie);
GetAuthenticationManager().SignIn(identity);
}
private IAuthenticationManager GetAuthenticationManager()
{
var ctx = Request.GetOwinContext();
return ctx.Authentication;
}
public ActionResult LogOut()
{
var ctx = Request.GetOwinContext();
var authManager = ctx.Authentication;
Session.Abandon();
authManager.SignOut("ApplicationCookie");
return RedirectToAction("LogIn", "Login");
}