gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:36:47 24 4
gpt4 key购买 nike

我想在他从该客户端注销后将用户重定向到同一个客户端。因此,如果我在一台身份服务器上有 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"

}

最佳答案

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

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

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

options.SaveTokens = true;

这会将 token 保留在 cookie 中。

为了自动重定向回客户端,对示例代码进行一些调整。在 IdentityServer AccountOptions
AutomaticRedirectAfterSignOut = true;

AccountController.Logout方法添加以下行:
if (vm.AutomaticRedirectAfterSignOut && 
!string.IsNullOrWhiteSpace(vm.PostLogoutRedirectUri))
return Redirect(vm.PostLogoutRedirectUri);

就在最后一行之前:
return View("LoggedOut", vm);

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

关于c# - 从身份服务器注销后如何将用户重定向到客户端应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56477130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com