IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

External provider Login , how to get id_token after external login

Open josipN opened this issue 9 years ago • 9 comments

Hello, In our application we are using your connect/token/ endpoint to recive an refresh_token and access_token which is later used as a JWT token for every request.

We want to add external login providers.

After setting up the external twitter provider we recive user_info which contains a large number of properties (name, lastname, email ect.). We can successfully log the user in using _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);

After this step we want to get the id_token from IdentityServer so that we can use it as JWT. We aren't sure how to connect to the /connect/token endpoint without the user password.

  [HttpGet("ExternalLogin")]
        [AllowAnonymous]
        // [ValidateAntiForgeryToken]
        public IActionResult ExternalLogin(string provider, string returnUrl = null)
        {
            // Request a redirect to the external login provider.
            var redirectUrl = Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl });
            var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
            return Challenge(properties, provider);
        }

        [HttpGet("ExternalLoginCallback")]
        [AllowAnonymous]
        public async Task<IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                return BadRequest("Authentication error");

            }
            var info = await _signInManager.GetExternalLoginInfoAsync();
            if (info == null)
            {
                //return RedirectToAction(nameof(Login));
                return BadRequest("Authentication error");
            }

            // Sign in the user with this external login provider if the user already has a login.
            // await HttpContext.Authentication.Exte("6Cookies", info.Principal);
            var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false);
....

Startup.cs
   app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationScheme = "Cookies",
                AutomaticAuthenticate = true,
                //AutomaticChallenge = false
            });
   app.UseTwitterAuthentication(new TwitterOptions
            {
                AuthenticationScheme = "Twitter",
                DisplayName = "Twitter",
                SignInScheme = "Identity.External",
                //SignInScheme = IdentityServer4.IdentityServerConstants.ExternalCookieAuthenticationScheme,
                ConsumerKey = "XXXXXXXXXXXXXXXXXXX",
                ConsumerSecret = "XXXXXXXXXXXXXXXXXXXX",
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                SaveTokens = true,
            });

josipN avatar May 10 '17 14:05 josipN

running into same issue, does anyone have any input ?

jerkovicl avatar May 11 '17 07:05 jerkovicl

You can't use both twitter and resource owner password from from the one client app. Those are mutually exclusive approaches.

brockallen avatar May 25 '17 15:05 brockallen

you can't get id_token with default Microsoft.AspNetCore.Authentication asp.net core framework provides. I come across the the same case. You want to use the default external login&callback process while get the hash fragment containing id_token to do something others. My solution is to resend request to get a response in //add code like described in link :https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oidc

var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false); if (result.Succeeded) { //add code using (HttpClient client = new HttpClient(httpClientHandler)) { } }

BitBridgeCoin avatar Jun 09 '17 02:06 BitBridgeCoin

I have tried to get access to URL fragments in aspnetcore without success. I gave up.

..tom

..Tom's phone

On Jun 8, 2017, at 7:52 PM, Jingkun Hu <[email protected]mailto:[email protected]> wrote:

you can't get id_token with default Microsoft.AspNetCore.Authentication asp.nethttp://asp.net core framework provides. I come across the the same case. You want to use the default external login&callback process while get the hash fragment containing id_token to do something others. My solution is to resend request to get a response in //add code like described in link :https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols-oidc

var result = await _signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, isPersistent: false); if (result.Succeeded) { //add code using (HttpClient client = new HttpClient(httpClientHandler)) { } }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/IdentityServer/IdentityServer4/issues/1151#issuecomment-307281401, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AKxq1gKwB9FsL_dxoLg-zDmQn76LLaARks5sCLNYgaJpZM4NWuWZ.

TomCJones avatar Jun 09 '17 03:06 TomCJones

(1) set url string: var token_endpoint = "https://login.microsoftonline.com/{tenant}/oauth2/authorize?"; var redirect_uri =here is a view; var client_id = "your client id"; var response_type = "id_token"; var response_mode = "form_post"; //var response_mode = ""; var scope = "openid"; var resource = ""; var state = "12345"; // random var nonce = "123"; // random var MockupString = token_endpoint + "response_type=" + response_type + "&response_mode"+response_mode+ "&client_id=" + client_id + "&redirect_uri=" + redirect_uri + "&nonce=2"; (2) then call url string in function like redirect(MockupString) (3) my case may be a bit more complex which use default callback process with identityserver4, and mix new account module with old one. this is my complete code for fetching id_token part that works. note you have a view corresponding to redirect_uri, then in your view url has the hash fragment that contain id_token, state, and the like

BitBridgeCoin avatar Jun 09 '17 04:06 BitBridgeCoin

Any update on the issue? closing for now - feel free to re-open if it needs further discussion.

leastprivilege avatar Jun 20 '17 15:06 leastprivilege

@brockallen You commented earlier saying

You can't use both twitter and resource owner password from the one client app. Those are mutually exclusive approaches.

Do you mean if I am using resource owner password grant for a client, then I can't use external authentication on the same client? If so, why is that?

stt106 avatar Jan 04 '18 21:01 stt106

Does anyone has an answer to stt106's question? I also have resource owner grant type for client and would like to use either domain login or log in with user name and password. For domain login I used the sample from IdentityServer4.Quickstart.UI but I don't know how to use this to access API.

klemencicd avatar Feb 06 '18 10:02 klemencicd

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 13 '20 17:01 lock[bot]