Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 602
Aran Mulholland Avatar asked Sep 19 '17 10:09

Aran Mulholland


1 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;
    }
like image 196
Victor Hugo Terceros Avatar answered Nov 15 '22 13:11

Victor Hugo Terceros