0

I have a pop up form in that form I load my Login view using jquery UI dialog .When the Login button is clicked I invoke the Account/Login action using ajax post .Here is my code :

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model)
{
    if (ModelState.IsValid)
    {
        var user = await UserManager.FindAsync(model.UserName, model.Password);
        if (user != null)
        {
           await SignInAsync(user, model.RememberMe);
           //IsAuthenticated is returns false 
           bool isAuthenticated= User.Identity.IsAuthenticated;
            return PartialView("_LoginPartial");
        }
        else
        {
            ModelState.AddModelError("", "Invalid username or password.");
        }
    }

    // If we got this far, something failed, redisplay form
    return View(model);
}

after the SignInAsync method is invoked ,the User.Identity.IsAuthenticated is returns false why thats happening?

Would appreciate any help offered. Let me know if you need any other information to help answer this question

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Mussammil
  • 864
  • 4
  • 16
  • 38

1 Answers1

4

This because the asp.net pipeline has not authenticated the user.

The User.Identity.IsAuthenticated value is based on HttpContext.Current. What should happen is SignInAsyncwill add a cookie that the framework will use to authenticate future requests.

So after this Controller action has been hit. User.Identity.IsAuthenticated will be true in all controller actions. The AuthorizeAttribute is based on this.

To get the username i would look at the user property that UserManager.FindAsync has returned i.e.

var userName = user.UserName
Rajdeep Dosanjh
  • 1,157
  • 1
  • 9
  • 20