What are the purposes of AuthenticationManager.SignIn vs AuthenticationManager.AuthenticateAsync?

Viewed 1682

AuthenticationManager has two methods.

void SignIn(params ClaimsIdentity[] identities);
Task<AuthenticateResult> AuthenticateAsync(string authenticationType);

What are their purposes? In what situations should they each be used?

2 Answers

I think the purposes are described on the names of the methods Authenticate and SignIn

So the purpose of AuthenticateAsync is basically get an Authentication Ticket

await ticket 
 = Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ExternalBearer);

it returns an AuthenticateResult like this

{Microsoft.Owin.Security.AuthenticateResult}
    Description: {Microsoft.Owin.Security.AuthenticationDescription}
    Identity: {System.Security.Claims.ClaimsIdentity}
    Properties: {Microsoft.Owin.Security.AuthenticationProperties}

and with this Result you can now SignIn (Add the Identity information to the context)

Context.Authentication.SignIn(ticket.Properties, ticket.Identity);

You can see it very clearly in the sample code below

var ticket = await Context.Authentication.AuthenticateAsync(Options.AuthenticationType);

    if(ticket != null)
    {
      Context.Authentication.SignIn(ticket.Properties, ticket.Identity);

      Response.Redirect(ticket.Properties.RedirectUri);

      return true;
    }

Here is the description of these two methods based on visual studio tool-tip. It may be useful.

AuthenticationManager.SignIn AuthenticationManager.AuthenticateAsync

Related