【问题标题】:How to redirect user to client app after logging out from identity server?从身份服务器注销后如何将用户重定向到客户端应用程序?
【发布时间】:2019-06-06 11:57:51
【问题描述】:

我想在用户从该客户端注销后将其重定向到同一客户端。因此,如果我在一台身份服务器上有 5 个客户端,我希望用户能够从一个客户端注销并在同一客户端上但已注销。

我尝试过的一件事是在快速入门的 AccountController 中使用 PostLogoutRedirectUri,但该值始终为空。我发现的解决方法是手动设置 PostLogoutRedirectUri,如果您在服务器上只有一个客户端,它可以正常工作,但如果我有多个客户端,则效果不佳。有没有办法知道哪个客户端被“注销”了?

  public async Task<IActionResult> Logout(LogoutInputModel model)
    {
        // build a model so the logged out page knows what to display
        var vm = await BuildLoggedOutViewModelAsync(model.LogoutId);

        if (User?.Identity.IsAuthenticated == true)
        {
            // delete local authentication cookie
            await HttpContext.SignOutAsync();

            // raise the logout event
            await _events.RaiseAsync(new UserLogoutSuccessEvent(User.GetSubjectId(), User.GetDisplayName()));
        }

        // check if we need to trigger sign-out at an upstream identity provider
        if (vm.TriggerExternalSignout)
        {
            // build a return URL so the upstream provider will redirect back
            // to us after the user has logged out. this allows us to then
            // complete our single sign-out processing.
            string url = Url.Action("Logout", new { logoutId = vm.LogoutId });

            // this triggers a redirect to the external provider for sign-out
            return SignOut(new AuthenticationProperties { RedirectUri = url }, vm.ExternalAuthenticationScheme);
        }


        vm.PostLogoutRedirectUri = "http://localhost:56582";
        return Redirect(vm.PostLogoutRedirectUri);
    }

我的客户

 new Client
                {

                    ClientId =  "openIdConnectClient",
                    ClientName = "Implicit Client Application Name",
                    AllowedGrantTypes = GrantTypes.Implicit,
                    AllowedScopes = new List<string>
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile,
                        IdentityServerConstants.StandardScopes.Email,
                        "role",
                        "customAPI.write"
                    },

                    RedirectUris = new List<string>{ "http://localhost:56582/signin-oidc" },
                    PostLogoutRedirectUris = new List<string>{ "http://localhost:56582" },
                   // FrontChannelLogoutUri = "http://localhost:56582/signout-oidc"

                }

【问题讨论】:

  • PostLogoutRedirectUri 是你为每个客户端配置的东西,如果你配置它不应该为空。您没有显示您的代码如何设置您的客户端。
  • 我已经用客户端代码更新了信息
  • 我有一个与 IdServer here 的开源集成,它在注销后会正确重定向。 source code here 我的 idserver 注销操作接受我与 IIdentityServerInteractionService.GetLogoutContext(logoutid) 一起使用的 logoutId,它为我返回客户端 PostLogoutRedirectUri。
  • 为什么我的 postlogoutredirecturi 在我的客户端中设置为空?
  • 你确定你得到了 logoutid 的值吗?也许看看我的代码herehere

标签: c# asp.net-core identityserver4


【解决方案1】:

您不应该手动设置 uri。实际上 IdentityServer 示例中的默认注销方法工作正常。

当您尝试3_ImplicitFlowAuthentication 示例项目时,您会看到PostLogoutRedirectUri 不为空并且重定向有效(但不是自动)。

在您的情况下PostLogoutRedirectUrinull 的原因可能是因为 id_token 没有保留。在MvcClient.Startup 中确保添加这一行:

options.SaveTokens = true;

这会将令牌保留在 cookie 中。

为了自动重定向回客户端,请对示例代码进行一些调整。在 IdentityServer 中AccountOptions 设置

AutomaticRedirectAfterSignOut = true;

AccountController.Logout 方法中添加以下行:

if (vm.AutomaticRedirectAfterSignOut && 
               !string.IsNullOrWhiteSpace(vm.PostLogoutRedirectUri))
    return Redirect(vm.PostLogoutRedirectUri);

就在最后一行之前:

return View("LoggedOut", vm);

当您再次运行示例时,您应该会看到用户现在在注销后自动返回到客户端。

【讨论】:

  • 感谢您的回答!我做了你所说的一切,但它仍然将我重定向到身份服务器,当我调试 vm.PostLogoutRedirectUri 时仍然为空。
  • 试试PostLogoutRedirectUris = { "http://localhost:56582/signout-callback-oidc" }
  • 是的,对不起,我是 stackoverflow 的新手
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 2016-07-25
  • 2022-01-01
  • 2017-09-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
相关资源
最近更新 更多