Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpContext.SignInAsync vs. SigninManger.SignInAsync

Can someone please explain to me what they are doing in background? I had problems when using HttpContext.SignInAsync with the SecurityStamp.

After using SigninManger.SignInAsync the error never occurred again.

Can you explain to me what the difference between these variants is? I want to understand why HttpContext.SignInAsync is behaving like this.

like image 949
Coder949 Avatar asked Jul 23 '19 20:07

Coder949


1 Answers

I was curious of the difference, so I checked the source code. (deep link)

SignInManager.SignInAsync is defined a below, where Context is HttpContext:

/// <summary>
/// Signs in the specified <paramref name="user"/>.
/// </summary>
/// <param name="user">The user to sign-in.</param>
/// <param name="authenticationProperties">Properties applied to the login and authentication cookie.</param>
/// <param name="authenticationMethod">Name of the method used to authenticate the user.</param>
/// <returns>The task object representing the asynchronous operation.</returns>
public virtual Task SignInAsync(TUser user, AuthenticationProperties authenticationProperties, string authenticationMethod = null)
{
    var additionalClaims = new List<Claim>();
    if (authenticationMethod != null)
    {
        additionalClaims.Add(new Claim(ClaimTypes.AuthenticationMethod, authenticationMethod));
    }
    return SignInWithClaimsAsync(user, authenticationProperties, additionalClaims);
}

public virtual async Task SignInWithClaimsAsync(TUser user, AuthenticationProperties authenticationProperties, IEnumerable<Claim> additionalClaims)
{
    var userPrincipal = await CreateUserPrincipalAsync(user);
    foreach (var claim in additionalClaims)
    {
        userPrincipal.Identities.First().AddClaim(claim);
    }
    await Context.SignInAsync(IdentityConstants.ApplicationScheme,
        userPrincipal,
        authenticationProperties ?? new AuthenticationProperties());
}

So it just calls HttpContext.SignInAsync with some options and add claim under some conditions

like image 99
Julian Avatar answered Sep 27 '22 22:09

Julian