C# 如何在从identity server注销后将用户重定向到客户端应用程序?

C# 如何在从identity server注销后将用户重定向到客户端应用程序?,c#,asp.net-core,identityserver4,C#,Asp.net Core,Identityserver4,我想在用户从该客户端注销后将其重定向到同一客户端。所以,如果我在一个identity server上有5个客户端,我希望用户能够从一个客户端注销,并在同一个客户端上注销 我尝试过的一件事是在quickstart中的AccountController中使用PostLogoutRedirectUri,但该值始终为null。我发现的解决方法是手动设置PostLogoutRedirectUri,如果服务器上只有一个客户机,这可以很好地工作,但如果我有多个客户机,情况就不是这样了。有没有办法知道哪个客户端

我想在用户从该客户端注销后将其重定向到同一客户端。所以,如果我在一个identity server上有5个客户端,我希望用户能够从一个客户端注销,并在同一个客户端上注销

我尝试过的一件事是在quickstart中的AccountController中使用PostLogoutRedirectUri,但该值始终为null。我发现的解决方法是手动设置PostLogoutRedirectUri,如果服务器上只有一个客户机,这可以很好地工作,但如果我有多个客户机,情况就不是这样了。有没有办法知道哪个客户端已经“注销”

公共异步任务注销(LogoutInputModel)
{
//构建一个模型,以便注销页面知道要显示什么
var vm=await BuildLoggedOutViewModelAsync(model.LogoutId);
if(User?.Identity.IsAuthenticated==true)
{
//删除本地身份验证cookie
等待HttpContext.SignOutAsync();
//引发注销事件
wait_events.RaiseAsync(新用户logoutSuccessEvent(User.GetSubjectId(),User.GetDisplayName());
}
//检查是否需要在上游身份提供商处触发注销
if(vm.TriggerExternalSignot)
{
//构建一个返回URL,以便上游提供程序将重定向回
//在用户注销后发送给我们。这允许我们
//完成我们的单一注销处理。
字符串url=url.Action(“注销”,新的{logoutId=vm.logoutId});
//这将触发重定向到外部提供程序以进行注销
返回注销(新的AuthenticationProperties{RedirectUri=url},vm.ExternalAuthenticationScheme);
}
vm.postlogutredirecturi=”http://localhost:56582";
返回重定向(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"

                }
新客户端
{
ClientId=“openIdConnectClient”,
ClientName=“隐式客户端应用程序名称”,
AllowedGrantTypes=GrantTypes.Implicit,
AllowedScopes=新列表
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
“角色”,
“customAPI.write”
},
重定向URI=新列表{”http://localhost:56582/signin-oidc“},
PostLogoutRedirectUris=新列表{”http://localhost:56582" },
//FrontChannelLogoutUri=”http://localhost:56582/signout-oidc“
}

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

当您尝试示例项目时,您将看到
PostLogoutRedirectUri
不为null,并且重定向工作(但不会自动)

在您的案例中,
postlogutredirecturi
null
的原因可能是因为没有保留id\u标记。在中,确保添加此行:

options.SaveTokens = true;
这将把代币保存在cookie中

为了自动重定向回客户机,对示例代码进行一些调整。在IdentityServer集合中

在方法中添加以下行:

if (vm.AutomaticRedirectAfterSignOut && 
               !string.IsNullOrWhiteSpace(vm.PostLogoutRedirectUri))
    return Redirect(vm.PostLogoutRedirectUri);
就在最后一行之前:

return View("LoggedOut", vm);

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

PostLogoutRedirectUri是您为每个客户端配置的内容,如果您配置它,它不应为null。您没有显示您的代码客户端是如何设置的。我已使用客户端代码更新了信息。我与IdServer进行了开源集成,在注销后可以正确重定向。我的idserver注销操作接受我与IIdentitityServerInteractionService.GetLogoutContext(logoutId)一起使用的logoutId,并为我返回客户端PostLogoutRedirectUri。当我在客户端中设置它时,为什么我的PostLogoutRedirectUri为null?您确定获得logoutId的值吗?也许看看我的代码,谢谢你的回答!我照你说的做了,但它仍然将我重定向到identity server,当我调试vm时。PostLogoutRedirectUri仍然为空。请尝试
PostLogoutRedirectUris={”http://localhost:56582/signout-回调oidc“}
。是的,对不起,我是stackoverflow的新手
if (vm.AutomaticRedirectAfterSignOut && 
               !string.IsNullOrWhiteSpace(vm.PostLogoutRedirectUri))
    return Redirect(vm.PostLogoutRedirectUri);
return View("LoggedOut", vm);