IdentityServer4 PostLogoutRedirectUri
Asked Answered
M

6

19

I am confused about how this is used.

Most examples I've seen have it given as "/signout-callback-oidc". That seems to indicate that it uses OIDC middleware in the process. What if I want to return to a specific client page?

The automatic redirect isn't working when I set IdentityServer's AccountOptions.cs property of AutomaticRedirectAfterSignOut to true. Further, during logout, I do not receive the client's PostLogoutRedirectUri.

So, is that link supposed to go to the OIDC middleware, or is it available for use to redirect to the client?

Misappropriate answered 5/3, 2018 at 18:36 Comment(2)
In the sample host for IdentityServer4, in the AccountService.BuildLoggedOutViewModelAsync, PostLogoutRedirectUri remains null. ClientName is also null. I get the external provider and logout ID only.Misappropriate
In fact, I do not have ANY context that tells me what the client is after that login interaction. During logout, IdentityServer4 does not tell me what client the user is coming from. All of that is null.Misappropriate
O
13

Your client has to be configured to request the callback to one of those URIs as part of the client-initiated sign-out flow.

IS4 clients can be configured with lists of allowable redirect URIs for both sign-in and sign-out, which I'm guessing is where you see /signout-callback-oidc -- if I remember right, either the docs or maybe the Quickstart code uses that, but there's nothing special about that particular URI name. (It isn't some OIDC standard, or a "well-known" name, or anything of that nature, as far as I know.)

The missing piece of the puzzle is to configure OIDC in the client application. You didn't mention what kind of application is on the client side, but in ASP.NET Core it's an option named SignedOutCallbackPath on the AddOpenIdConnect service:

services.AddOpenIdConnect("oidc", options =>
{
    options.SignInScheme = "Cookies";
    options.Authority = appConfig["OidcAuthority"];
    options.ClientId = appConfig["OidcClientId"];
    options.ClientSecret = appConfig["OidcClientSecret"];
    // etc

    options.SignedOutCallbackPath = "/jake-says-goodbye";
});

This causes the OIDC implementation to add a property to the sign-out request identifying that redirect URI. As long as your application properly identifies itself, as briefly mentioned in the docs here, and as long as /jake-says-goodbye is one of the approved post-logout redirect URIs on the IS4 side, you should get the callback you're expecting.

(I specifically mention "proper" identification because, based on github questions I've seen, it sounds like it might be more difficult to manage for a JS-based SPA client app versus whatever helpful things MVC does behind the scenes to manage server-to-server OIDC interaction. I can't speak to that as I've not had a need to implement any SPA clients with IS4 yet.)

Opinionated answered 5/3, 2018 at 20:10 Comment(7)
My client is configured as such, on both client and server side. When debugging through the flow of the logout on the IdentityServer host side, I do not see that it has access to any context with client information regarding that request (maybe it's there, I just don't know where to look). I did find in the MVC Owin Hybrid client startup where it's sending the id_token_hint and logoutid (when it's a logout request), and I added postlogout there as well: n.ProtocolMessage.PostLogoutRedirectUri = myPostRedirectURI; No matter what, the postlogoutredirecturi on IdSrvr side is still null.Misappropriate
When you're debugging into IS4 during sign-out, you should be able to get the context from the interaction service (inject it), I think it's called GetAuthorizationContext -- sorry, not at a computer with that code. If that succeeds, you should be able to read a clientId property off that and verify whether the return URI is mapping back to the client.Opinionated
Thanks for the pointers. I did that, but in order to use GetAuthorizationContext, I have to have a returnUrl. If that's for the client, I have multiple clients, each with a different returnUrl, and no context to pull which client is hitting IdSrv. I tried using the server's return url, no good. Comes back null.Misappropriate
The client I have is made from the base code in IdentityServer4's client samples - MVC Owin Hybrid. Essentially, I'm hitting the same issue as this: github.com/IdentityServer/IdentityServer4/issues/992 But comparing my code to lemieuxs's didn't highlight anything I don't already have.Misappropriate
Thanks for the help. The end result (got it working) is in my answer below.Misappropriate
I have marked this as the answer, and in my below answer documented further what I did.Misappropriate
With the code I'm running, my experience is that the PostLogoutRedirectUri value must match a record in [identityserver].[ClientPostLogoutRedirectUris] for our ClientId, otherwise IdentityServer ignores it and instead redirects to Identity/Account/SignedOut (in IdSvr). It could be that this table feeds the configuration mentioned above, I'm not sure.Costello
M
6

The problem is that you have to set a very specific parameter in order for the PostLogoutRedirectUri to not show up as null on IdentityServer's side, and testing any of the options results in having to step through a ton of ways to set it, most of them still resulting in null. Since I'm using an older client with IdentityServer4 (in order to enable .NET 4.x webapps to authenticate through IdentityServer4, cannot easily use .NET Core with those projects - luckily IdentityServer4 is still compatible with the older client code), the action that triggers signout has two relevant things (and you'll find a TON of examples of code for that will not work for you with MVC in .NET 4.x):

Use the signout() method in this sample github repo (the IdentityServer3 MVC Owin sample client): https://github.com/IdentityServer/IdentityServer3.Samples/blob/master/source/Clients/MVC%20OWIN%20Client/Controllers/HomeController.cs You can trigger that action from a button in a view.

That will get intercepted by the client's Owin middleware if you do this: https://github.com/IdentityServer/IdentityServer3/issues/2687#issuecomment-194739035 I didn't use the stored message bit, and I added the PostLogoutRedirectUri parameter in a way that IdentityServer4's LogoutRequest model wouldn't remove with this line in the same segment:

n.ProtocolMessage.PostLogoutRedirectUri = "http://myredirectaddress/ActionToDoOnceReturned"; 

You have to make sure that the above matches the client's PostLogoutRedirectUri on the IdentityServer side's client config or it'll be null again, and you would have missed it among all the other parameters. For instance, these methods of setting PostLogoutRedirectUri DO NOT work:

n.ProtocolMessage.SetParameter("PostLogoutRedirectURI", "some URL");
n.ProtocolMessage.SetParameter("PostLogoutUri", "another URL");
n.ProtocolMessage.SetParameter("PostLogoutRedirectUri", "yet another URL that's going to be ignored by IdentityServer4");

From there, you're off to the races because PostLogoutRedirectUri is no longer null. There are a few more considerations: check out AccountOptions in the IdentityServer controller folder. I set AutomaticRedirectAfterSignout to true there (this is used by Javascript in IdSrv's final logout page - if set, the script uses PostLogoutRedirectUri to forward the user back to the client). There's also an option to show a logout confirmation prompt, which if you want to actually display, make sure to NOT set the id token hint in the Owin (it's right next to where we set the PostLogoutRedirectUri / the part that gets triggered by signout requests). If you do those two things, AccountServices.BuildLogoutViewModel will return the prompt to the user when it's called by the AccountController.logout() method. Thank you aaronR for the answer to my other question concerning that part:
IdentityServer4 logout (id token hint tells IdentityServer that the signout request was authorized and not a malicious person trying to harass your system / sign out users, IdSrv will ask the user for confirmation if it's not provided).

Finally, if you are confused by what's happening on the IdentityServer side in logout, and why it's repeatedly triggering the same method:

First time it gets called from the client's Owin middleware (the bit of code above that gets triggered after the Signout() action).

It uses AccountService to build a view model to return to the user for logout confirmation.

It gets triggered again by the user clicking yes on that page.

It goes through the Account service method again, which this time sets the bool to show the logout confirmation to false.

It calls the second logout method, the one with the view model that gets passed in.

This one triggers the external identity provider signout.

The external identity provider returns control back to logout, resulting in it getting called again, calling the second logout method again.

Finally, it will return the user to IdentityServer's logout page. If PostLogoutRedirectUri is set & AutomaticRedirectAfterSignOut is true, there's javascript on that page which automatically forwards the user's browser to it.

Due to having two projects to debug through at once and all of these possible ways of setting the redirect (which also have to match client/server side config in order not to be null) it can be easy to get confused.

Misappropriate answered 7/3, 2018 at 16:54 Comment(2)
Why is this so difficult?Pintail
I have the same redirect uri defined in the db on id svr side and in side my demo client - i have same setup as you too. Demo .net 4.X MVC Client with OWIN. Cannot get the redirect uri to populate.Sipes
E
4

Overview

When setting up IdentityServer (assuming it's a separate application), there are two parameters in the configuration for an accessing client: RedirectUris and PostLogoutRedirectUris. These correspond to what happens after a login or logout of a user against the IdentityServer system.

Since your client app probably has its own cookies (or JWT tokens, or whatever it's using to maintain a user session), it needs to know when the IdentityServer has processed the login and made the user data available.

The default ASP.NET OpenID Connect middleware does this with yourapp.com/signin-oidc and yourapp.com/signout-callback-oidc endpoints to intercept and handle the login/logout hand-off from IdentityServer. These endpoints have nothing to do with the OpenID protocol and can be set to whatever you want if you have your own authentication handler, but if you're using the default middleware then you must set them to that in the IdentityServer config.

Config option

However, if you still want to redirect a user after the OpenID Connect logout has completed, there's an option specifically for this:

services.AddOpenIdConnect(options => 
{
     // your other options...

     options.SignedOutRedirectUri = "/some-page-after-oidc-logout";
});

Microsoft Docs

Elvia answered 17/9, 2018 at 3:14 Comment(0)
C
0

I want to share how I solved problem with null PostLogoutRedirectUri value. I always had null PostLogoutRedirectUri value in logout context until I added SignInScheme value on mvc client side. These settings of authentication on MVC client side works for me:

var authenticationBuilder = services.AddAuthentication(options =>
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
});

authenticationBuilder.AddCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromMinutes(60);
    options.Cookie.Name = "identity_server_mvc";
});

authenticationBuilder.AddOpenIdConnect("oidc", options =>
{
    options.Authority = "{IDENTITY_SERVER_URL}";
    options.ClientId = "mvc";
    options.SaveTokens = true;
    options.SignInScheme = "Cookies";
});

You also need to make sure that you have added the PostLogoutRedirectUri value to the client configuration on the Identity Server side:

new Client
{
    ClientId = "mvc",
    AllowedGrantTypes = GrantTypes.Implicit,

    RedirectUris           = { "{CLIENT_URL}/signin-oidc" },
    PostLogoutRedirectUris = { "{CLIENT_URL}/signout-callback-oidc" },

    AllowedScopes =
    {
        IdentityServerConstants.StandardScopes.OpenId,
        IdentityServerConstants.StandardScopes.Profile
    }
}

One last but important point, because of this I had null logoutId value on Identity Server side. To initiate Logout process you must first call SignOut("Cookies", "oidc") on mvc client side. Example endpoint in my HomeController:

public IActionResult Logout()
{
    return SignOut("Cookies", "oidc");
}

Good luck!

Chamois answered 28/10, 2019 at 23:41 Comment(0)
N
0

Building on top of @McGuireV10 answer if your project is a Blazor WASM, then the change would be like this:

// Adds OpenID Connect Authentication
builder.Services.AddOidcAuthentication(options =>
{
 options.ProviderOptions.Authority = settings.Authentication.Authority;
 options.ProviderOptions.ClientId = settings.Authentication.ClientId;
 options.ProviderOptions.ResponseType = "code";
 options.ProviderOptions.ResponseMode = "query";

 //
 options.AuthenticationPaths.LogOutCallbackPath = "authentication/logout-callback";

 builder.Configuration.Bind("oidc", options.ProviderOptions);
});
Neuromuscular answered 16/10, 2020 at 11:33 Comment(1)
I am using Blazor WASM and this did not work for me. PostLogoutRedirectURI is still null for me even though it matches the client config in IDS4 side. Is there anything else you had to configure? It would be nice to see your client-side "logout" call. Thanks, MitchGiddings
S
0

I ran into the same issue today; your (@JakeJ) link solved it for me. I am building a demo MVC Owin Client in .net 4.6.1 (for a third party company) so our set up is the same and our Id Svr v4 is built on net core v3.1.

I verified i had the same PostLogoutRedirectUri defined in the Id Svr side config for the client i was working on and then at the client side config too.

But i noticed that i could add a small block of code taken from the ref'ed github issue to the RedirectToIdentityProvider func delegate specific to logout.

if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
{
    // below is technically not needed, as it was already set for me.
    n.ProtocolMessage.PostLogoutRedirectUri = LoginAndOutRedirectUri;
    var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token").Value;
    n.ProtocolMessage.IdTokenHint = idTokenHint;
}

This means that a claim needs to be present in order for this to work so i then added the below to the SecurityTokenValidated func delegate:

// add id token for logout
currentIdentity.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));

I saw many examples where folks were populating the AuthenticationTicket inside the AuthorizationCodeReceived func delegate but for me it was always null. So some head scratching lead me to implementing what i needed inside the SecurityTokenValidated delegate. And it all works and hands together nicely.

Sipes answered 19/4, 2021 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.