Denis
Denis

Reputation: 6082

IdentityServer4 with external provider. Redirect to frontend page with id_token

I have configured IdentityServer4 with external providers like Facebook and Google. Also a have added default UI for Register, Login and other things. And my problem is how to redirect to the page with id_token? Now my flow is:

  1. Open Identity/Account/Login;
  2. Click Facebook;
  3. POST /Identity/Account/ExternalLogin
  4. Redirect to https://www.facebook.com/v4.0/dialog/oauth?client_id=xxx;
  5. Redirect to /signin-facebook?code=
  6. And now I'm redirected to /Identity/Account/ExternalLogin?returnUrl=%2F&handler=Callback

How to configure redirect from #5 step to specific page, not to /Identity/Account/ExternalLogin?returnUrl=%2F&handler=Callback? I need it for my frontend to continue working with API via oidc-client.

I seen it in Securing Angular Apps with OpenID and OAuth2 pluralsight course in 3.10 video, but Brian Noyes didn't use external provider.

Upvotes: 0

Views: 2146

Answers (1)

Nan Yu
Nan Yu

Reputation: 27538

The steps 2-6 are on Identity Server application , not your client app . When clicking Facebook login , user will be redirected to facebook's login page and enter the credential , facebook will return code(if using code flow) to identity server app , and then identity server app will send a post request to facebook's token endpoint with code for exchanging id token/access token , after identity server app get id token , it will decode the token and get user's claims , then create identity server's own tokens and at last return to your client app .

Identity server will handle the external login in ExternalLogin method , you can't interrupt to return ID token to your client side during the authentication flow . If you need the facebook's ID token , you can cache the code in ExternalLogin method , and at last return to client app , for example , add token to token response . Check this code sample and check whether it helps.

Updated :

To get id token in Callback function of ExternalLogin , you can try below steps :

  1. set options.SaveTokens = true; in AddOpenIdConnect config of Facebook login in Identity Server .

  2. Use below codes in Callback function to get id token :

    var result = await HttpContext.AuthenticateAsync(IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme);
    
    var tokens = result.Properties.GetTokens();
    var idToken = tokens.Where(x => x.Name.Equals("id_token")).FirstOrDefault().Value;
    

Upvotes: 1

Related Questions