从身份服务器注销后如何将用户重定向到客户端应用程序?

Vel*_*lic 2 c# asp.net-core identityserver4

我想在用户从该客户端注销后将其重定向到同一个客户端。因此,如果我在一台身份服务器上有 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);
    }
Run Code Online (Sandbox Code Playgroud)

我的客户

 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"

                }
Run Code Online (Sandbox Code Playgroud)

Rua*_*urg 7

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

当您尝试3_ImplicitFlowAuthentication示例项目时,您将看到PostLogoutRedirectUriis not null 并且重定向有效(但不是自动的)。

之所以PostLogoutRedirectUrinull你的情况可能是因为id_token不会被保留。在MvcClient.Startup 中确保添加以下行:

options.SaveTokens = true;
Run Code Online (Sandbox Code Playgroud)

这将在 cookie 中保留令牌。

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

AutomaticRedirectAfterSignOut = true;
Run Code Online (Sandbox Code Playgroud)

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

if (vm.AutomaticRedirectAfterSignOut && 
               !string.IsNullOrWhiteSpace(vm.PostLogoutRedirectUri))
    return Redirect(vm.PostLogoutRedirectUri);
Run Code Online (Sandbox Code Playgroud)

就在最后一行之前:

return View("LoggedOut", vm);
Run Code Online (Sandbox Code Playgroud)

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