0

I want to redirect back to my client after sign-out from local, then the IS4; My AspNetCore Mvc client works correctly and redirect back to the client after sign-out, but the AspNet Mvc (not Core) it doesn't.

here is my Startup.Configuration method:

    public void Configuration(IAppBuilder app)
    {           
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "Cookies",
        });

        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
        {
            SignInAsAuthenticationType = "Cookies",

            Authority = "https://localhost:5000",

            UseTokenLifetime = false,              
            // RedeemCode = true,

            ClientId = "aspNet_client",               
            ClientSecret = "secret",                
            RedirectUri = "https://localhost:44343/sigin-oidc",              
            PostLogoutRedirectUri = "https://localhost:44343/signout-callback-oidc",               
            SaveTokens = true,            
            ResponseType = "code id_token",

            Scope = "openid profile offline_access",

            TokenValidationParameters = new TokenValidationParameters()
            {
                NameClaimType = JwtClaimTypes.PreferredUserName,
                RoleClaimType = JwtClaimTypes.Role,
            },

            Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = onAuthenticationFailed,
                MessageReceived = onMessageReceived,

               // AuthorizationCodeReceived = onAuthorizationCodeReceived
            }
        });
    }

I used this method to sign-out:

public ActionResult SignOut()
{
    Request.GetOwinContext().Authentication.SignOut();
    return Redirect("/");
}

I used this method too:

    public ActionResult SignOut()
    {
         System.Web.HttpContext.Current.GetOwinContext().Authentication.SignOut(
                    new AuthenticationProperties
                    {
                        RedirectUri = "https://localhost:44343"
                    },
                        CookieAuthenticationDefaults.AuthenticationType,
                        OpenIdConnectAuthenticationDefaults.AuthenticationType
                    );

          //"Cookies", "OpenIdConnect"
    }

But not worked. So my question is: How to automatic redirect back to my AspNetMvc Client after sign-out?

1 Answer 1

1

This was an error reported long time ago on IdentityServer3. It got fixed here by setting IdTokenHint on logout. In this case as we use IdentityServer4, we can implement similar fix manually on ASP.NET MVC app. Here is changes need to make:

  1. on IdentityServer project set PostLogoutRedirectUris for the client:
new Client
{
   ClientId = "aspNet_client",

   //All other settings ...

   PostLogoutRedirectUris = { "http://localhost:44343" },
},
  1. On ASP.NET mvc application, set OpenIdConnectAuthenticationOptions - PostLogoutRedirectUri to the same value as step 1

  2. Change Notifications - SecurityTokenValidated and RedirectToIdentityProvider to set IdTokenHint on logout

app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
            {
                // other settings...

                PostLogoutRedirectUri = "http://localhost:44343",
                Notifications = new OpenIdConnectAuthenticationNotifications
                {
                    SecurityTokenValidated = n =>
                    {
                        n.AuthenticationTicket.Identity.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
                        return Task.FromResult(0);
                    },
                    RedirectToIdentityProvider = n =>
                    {
                        if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
                        {
                            var id_token_claim = n.OwinContext.Authentication.User.Claims.FirstOrDefault(x => x.Type == "id_token");
                            if (id_token_claim != null)
                            {
                                n.ProtocolMessage.IdTokenHint = id_token_claim.Value;
                            }
                        }
                        return Task.FromResult(0);
                    }
                }
            });
  1. If you want to redirect automatically set AccountOptions - AutomaticRedirectAfterSignOut to true on IdentityServer, default value is false.

Implemented it myself here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.