IdentityServer4 - redirect to MVC client after logout

I use IdenetityServer4 and redirecting to the MVC client after logging out does not work. The following action of my MVC client controller Logout:

public async Task Logout() { await HttpContext.Authentication.SignOutAsync("Cookies"); await HttpContext.Authentication.SignOutAsync("oidc"); } 

The following is the authentication server host configuration file 4.

 public static IEnumerable<Client> GetClients() { return new List<Client> { // other clients omitted... // OpenID Connect implicit flow client (MVC) new Client { ClientId = "mvc", ClientName = "MVC Client", AllowedGrantTypes = GrantTypes.Implicit, // where to redirect to after login RedirectUris = { "http://localhost:58422/signin-oidc" }, // where to redirect to after logout PostLogoutRedirectUris = { "http://localhost:58422/signout-callback-oidc" }, AllowedScopes = new List<string> { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile } } }; } 

I want the user to be redirected back to the MVC client after exiting IdentityServer. Right now, the user should click the show link in the image below to redirect back to the MVC site, but I think the user should be automatically redirected back to the MVC client.

enter image description here

+10
source share
3 answers

There is no problem in your Config.cs or in the MVC controller.

Go to your IdentityServer4 application, and then, inside the method for logging out of your AccountController [HttpPost], make the following changes:

 public async Task<IActionResult> Logout(LogoutViewModel model) { ... //return View("LoggedOut", vm); return Redirect(vm.PostLogoutRedirectUri); } 

This will redirect the user back to the MVC application (in your case).

There is a better way to do this: You can set these parameters from AccountOptions.cs as follows:

 public static bool ShowLogoutPrompt = false; public static bool AutomaticRedirectAfterSignOut = true; 
+18
source

If someone uses Scaffolding (they use Razor Page files), here's how to fix it according to Akhilesh's answer:

In the \ Identity \ Pages \ Account \ Logout.cshtml areas:

First add the IIdentityServerInteractionService service:

  IIdentityServerInteractionService _interaction; public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger, IIdentityServerInteractionService _interaction) { _signInManager = signInManager; _logger = logger; this._interaction = _interaction; } 

You may need to add OnGet() support, the logic may vary depending on your case, in my case, Get or Post does not matter:

  public async Task<IActionResult> OnGet(string returnUrl = null) { return await this.OnPost(returnUrl); } 

Add LogoutId logic to OnPost:

  public async Task<IActionResult> OnPost(string returnUrl = null) { await _signInManager.SignOutAsync(); _logger.LogInformation("User logged out."); var logoutId = this.Request.Query["logoutId"].ToString(); if (returnUrl != null) { return LocalRedirect(returnUrl); } else if (!string.IsNullOrEmpty(logoutId)) { var logoutContext = await this._interaction.GetLogoutContextAsync(logoutId); returnUrl = logoutContext.PostLogoutRedirectUri; if (!string.IsNullOrEmpty(returnUrl)) { return this.Redirect(returnUrl); } else { return Page(); } } else { return Page(); } } 
+5
source

Luke's code works when I enter and exit without exiting the application, but it does not work with the following script:

  1. I launch the application and log in.

  2. I exit the application

  3. I restart the application, exit

=> logoutId is zero

=> return Page () instead of return this.Redirect (returnUrl)

How to get logoutId for this second script?

0
source

Source: https://habr.com/ru/post/1266425/


All Articles