IdentityServer / IdentityServer3

OpenID Connect Provider and OAuth 2.0 Authorization Server Framework for ASP.NET 4.x/Katana
https://identityserver.github.io/Documentation/
Apache License 2.0
2.01k stars 764 forks source link

Sign-in with Resource Owner Flow and use token for Implicit Flow through new identity creation. #3434

Closed myaesubi closed 7 years ago

myaesubi commented 7 years ago

Question

Hello, Please let me know if this is a general question or a dumb question! as I'm struggling with this...

In IdentityServer3, I've used the code in 'Console Resource Owner Flow with UserInfo' sample to use my own login credential to get the JWT token from identityserver.

static TokenResponse RequestToken()
        {
           `var client = new TokenClient(
                Constants.TokenEndpoint,
                "ro.client",
                "secret");

            return client.RequestResourceOwnerPasswordAsync("bob", "bob", "openid email").Result;
        }

Using above code in "MVC OWIN Client (Implicit)" homecontroller/Test action as following:

var response = RequestToken();
            var token = response.AccessToken;
            var jwt = new JwtSecurityToken(token);
            var identity = new ClaimsIdentity("ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
            foreach (var c in jwt.Claims)
            {
                var t = c.Type;
                var v = c.Value;

                identity.AddClaim(new Claim(t, v));

            }

            IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
            authenticationManager.SignOut("ApplicationCookie");
            authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

            return Redirect("Claims");

Even though the OWIN middleware now has new identity, the redirect to "Claims" action which is secured through "Authorize" attribute, still takes me back to login page in IdentityServer.

I looked to the logs, as I enabled verbose mode looks like the following

iisexpress.exe Information: 0 : 2016-12-29 23:42:19.158 -05:00 [Information] User is not authenticated. Redirecting to login. iisexpress.exe Information: 0 : 2016-12-29 23:42:19.167 -05:00 [Information] End authorize request iisexpress.exe Information: 0 : 2016-12-29 23:42:19.168 -05:00 [Information] Redirecting to login page 2016-12-29 23:42:19.174 -05:00 [Debug] Protecting message: "{\"ReturnUrl\":\"https://localhost:44333/core/connect/authorize?client_id=mvc.owin.implicit&redirect_uri=https%3A%2F%2Flocalhost%3A44301%2F&response_mode=form_post&response_type=id_token&scope=openid%20email&state=OpenIdConnect.AuthenticationProperties%3DEgPDCJbtQK4zrCnZWHcUxGixx3Q-7stHejVVPSRRkv3iPlINdaxpdPvok1Q_x2LLSS5JRNU9WWFwJ51np5hZSyBytH05OyNkp-T1ssYFHpznRLgBC88wsANH-IzDT1RHDkCGi0T-EgBRSNuULT9FkpGkBTOSFpW_q1Y_Iu_CB5Cmut9vdjKwU8iv9tfYwSR4f2wrLBdWE3wcisdjEjvISA&nonce=636186697390743516.MjBiZjQwZGUtODU3MS00Nzk5LWEzOTItN2U4NTYyZTAyNDQxOWMyODJkYjEtOWE0ZC00NTI1LTg2MDEtOTY2OGMwMjQyOTQ5\",\"ClientId\":\"mvc.owin.implicit\",\"AcrValues\":[],\"Created\":636186697391013500}" iisexpress.exe Information: 0 : 2016-12-29 23:42:19.223 -05:00 [Information] Login page requested 2016-12-29 23:42:19.260 -05:00 [Debug] signin message passed to login: "{ \"ReturnUrl\": \"https://localhost:44333/core/connect/authorize?client_id=mvc.owin.implicit&redirect_uri=https%3A%2F%2Flocalhost%3A44301%2F&response_mode=form_post&response_type=id_token&scope=openid%20email&state=OpenIdConnect.AuthenticationProperties%3DEgPDCJbtQK4zrCnZWHcUxGixx3Q-7stHejVVPSRRkv3iPlINdaxpdPvok1Q_x2LLSS5JRNU9WWFwJ51np5hZSyBytH05OyNkp-T1ssYFHpznRLgBC88wsANH-IzDT1RHDkCGi0T-EgBRSNuULT9FkpGkBTOSFpW_q1Y_Iu_CB5Cmut9vdjKwU8iv9tfYwSR4f2wrLBdWE3wcisdjEjvISA&nonce=636186697390743516.MjBiZjQwZGUtODU3MS00Nzk5LWEzOTItN2U4NTYyZTAyNDQxOWMyODJkYjEtOWE0ZC00NTI1LTg2MDEtOTY2OGMwMjQyOTQ5\", \"ClientId\": \"mvc.owin.implicit\", \"IdP\": null, \"Tenant\": null, \"LoginHint\": null, \"DisplayMode\": null, \"UiLocales\": null, \"AcrValues\": [], \"Created\": 636186697391013500 }"

Thank in advance, Mohammad

brockallen commented 7 years ago

This seems to be a general question about IdentityServer - not a bug report or an issue.

Please use StackOverflow for that. This has the advantage that questions and answers can be easily found by search engines, and that there are more people answering questions than just us.

For IdentityServer3 https://stackoverflow.com/questions/tagged/?tagnames=identityserver3&sort=newest

For IdentityServer4 https://stackoverflow.com/questions/tagged/?tagnames=identityserver4&sort=newest

For commercial support https://identityserver.io/