What does User.Identity.Name and User.Identity.IsAuthenticated set?

I want to know what set the username and changed the value isAuthenticated

to true.

Why is User.Identity.Name

empty string and User.Identity.IsAuthenticated

false

after return SignInManager.PasswordSignInAsync

Success

.

// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var userIdentityNameTest = User.Identity.Name; // Empty string

    var result = await SignInManager.PasswordSignInAsync(
                                             model.Email, model.Password, 
                                             model.RememberMe, shouldLockout: false);
    // result is "Success"

    userIdentityNameTest = User.Identity.Name;
    // userIdentityNameTest is still an empty string?
    // User.Identity.IsAuthenticated is still false?

    switch (result)
    {
        case SignInStatus.Success:
            return RedirectToLocal(returnUrl);
        case SignInStatus.LockedOut:
            return View("Lockout");
        case SignInStatus.RequiresVerification:
            return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, 
                                                RememberMe = model.RememberMe });
        case SignInStatus.Failure:
        default:
            ModelState.AddModelError("", "Invalid login attempt.");
            return View(model);
    }
}

      

+3


source to share


1 answer


It seems to SignInManager.PasswordSignInAsync

only validate the input and run AuthenticationManager.SignIn

it if you are not using TwoFactorAuthentication

. AuthenticationManager.SignIn

in this case, only sets the authentication cookie.

So, it User.Identity

is available in subsequent requests to your application. To get ApplicationUser

on Name

, you can use ApplicationUserManager

like this:



UserManager.FindByNameAsync(model.Name)

+4


source







All Articles