Redirect other then Home/Index using OpenIdConnectAuthentication and Identity server after login

自古美人都是妖i 提交于 2019-12-25 14:24:33

问题


I'm trying to Redirect user to Dashboard but it always redirect it to Home/Index that is because I've set RedirectUri to http://localhost:35641/ in Identity Server Options. But that is true in case of application landing page after login it needs to redirect o dashboard. I can write custom logic in Index's Action Result but I want to avoid it. MVC web Startup method

  public void Configuration(IAppBuilder app)
    {
                // Implicit mvc owin
                JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = "Cookies"
                });
                app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
                {
                    ClientId = ApplicationConstants.ClientIdNucleusMvcApp,
                    Authority = ApplicationConstants.UrlBaseAuth,
                    RedirectUri = ApplicationConstants.UrlBaseWeb,
                    PostLogoutRedirectUri = ApplicationConstants.UrlBaseWeb,
                    ResponseType = "id_token token",
                    Scope = string.Format("openid email {0}", ApplicationScopes.MvcApp),
                    SignInAsAuthenticationType = "Cookies",

                    // sample how to access token on form (when adding the token response type)
                    Notifications = new OpenIdConnectAuthenticationNotifications
                    {
                        SecurityTokenValidated = async n =>
                        {
                            // Adding access token in claims
                            var accessToken = n.ProtocolMessage.AccessToken;
                            if (!string.IsNullOrEmpty(accessToken))
                            {
                                n.AuthenticationTicket.Identity.AddClaim(new Claim("access_token", accessToken));
                            }

                            // Adding identity token in claims
                            var identityToken = n.ProtocolMessage.IdToken;
                            if (!string.IsNullOrEmpty(identityToken))
                            {
                                n.AuthenticationTicket.Identity.AddClaim(new Claim("identity_token", identityToken));
                            }
                        },
                        RedirectToIdentityProvider = async n =>
                        {
                            // if signing out, add the id_token_hint
                            if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.LogoutRequest)
                            {
                                var idToken = n.OwinContext.Authentication.User.FindFirst("identity_token");
                                n.ProtocolMessage.IdTokenHint = idToken == null ? null : idToken.Value;
                                n.ProtocolMessage.PostLogoutRedirectUri = ApplicationConstants.UrlBaseWeb;
                            }
                        }
                    }
                });
            }

Here is my Client on Identity Server

 new Client
                {
                    Enabled = true,
                    ClientName = ApplicationConstants.ClientNameNucleusMvcApp,
                    ClientId = ApplicationConstants.ClientIdNucleusMvcApp,
                    ClientSecrets = new List<ClientSecret>
                    {
                        new ClientSecret(ApplicationConstants.ClientSecretNucleusMvcApp.Sha256())
                    },
                    Flow = Flows.Implicit,
                    RequireConsent = false,
                    AccessTokenType = AccessTokenType.Reference,
                    IdentityTokenLifetime = 1800,
                    AccessTokenLifetime = 1800,
                    RedirectUris = new List<string>
                    {
                        // MVC form post sample
                        ApplicationConstants.UrlBaseWeb,
                        ApplicationConstants.UrlBaseWeb + "Dashboard/Index"
                    },
                    PostLogoutRedirectUris = new List<string>
                    {
                        ApplicationConstants.UrlBaseWeb
                    }
                }

Help will be appreciated. Thanks


回答1:


The RedirectUri you use for talking with your authority should not make a difference, that's just used for dispatching the token back to your application. After that there is an internal (==local to the app) redirect that is used for setting the session cookie and can go anywhere you want within the site. How do you trigger authentication? If you started from a protected action via [authorize], you should always land back in there in the end. If you are using explicit sign in code like if

HttpContext.GetOwinContext().Authentication.Challenge(new AuthenticationProperties { RedirectUri = "/" }, OpenIdConnectAuthenticationDefaults.AuthenticationType);

you can always specify whatever desired landing route you want in RedirectUri. I know, it is fantastically confusing that the property driving this internal redirect has the exact same name as the protocol counterpart - the only excuse we have is that the AuthenticationProperties class already existed when the new claims based middleware was introduced, and calling the actual OAuth/OIDC redirect_uri with the underscore didn't fly with the .NET community. HTH



来源:https://stackoverflow.com/questions/32518606/redirect-other-then-home-index-using-openidconnectauthentication-and-identity-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!