【问题标题】:How to sign-out using the end session endpoint IdentityServer4?如何使用结束会话端点 IdentityServer4 注销?
【发布时间】:2019-05-23 13:07:37
【问题描述】:

我尝试使用来自 IdentityServer4 文档的 GET 请求从我的会话中注销。 HttpResponseMessage 看起来像这样:

HttpResponseMessage res = await client.GetAsync($"connect/endsession?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}");

起初,我对 Uri 长度有疑问。当我发送请求时,方法捕获异常

Invalid URI: The Uri scheme is too long. 

为了解决这个问题,我尝试将参数发送到这样的字符串中:

var parameters = $"?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}";
HttpResponseMessage res = await client.GetAsync("connect/endsession" + parameters );

还可以像这样在 Program.cs 中添加 MaxRequestLineSize

UseKestrel(options =>
{
  options.Limits.MaxRequestLineSize = 20480;
})

也尝试过这种方式:https://stackoverflow.com/a/32457474/9541386 但对我没有任何作用。

我已尝试通过 Postman 发送此请求。请求已发送

http://localhost:5000/connect/endsession?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}

但在 IClientStore 接口 clientId 参数的 FindClientByIdAsync 方法中,如下所示:

但正常情况下是有Id的。我看不到它之前会发生什么,因为它是第一个入口点。 如何解决 Uri 长度和错误参数的问题?

【问题讨论】:

    标签: c# asp.net identityserver4


    【解决方案1】:

    问题很可能是查询参数中的无效字符:

    ?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}
    

    在这种情况下,我怀疑字符串 postLogoutRedirectUri 包含字符 :,如果不转义则无效:%3A

    对 uri 进行编码:

    var encodedUri = System.Web.HttpUtility.UrlEncode(postLogoutRedirectUri);
    

    并将其用作参数:

    ?id_token_hint={idTokenHint}&post_logout_redirect_uri={encodedUri}
    

    虽然这可能会解决问题,但您为什么不使用provided methods to signout?例如:

    //using Microsoft.AspNetCore.Authentication;
    //using Microsoft.AspNetCore.Mvc;
    
    // Remove cookie
    await HttpContext.SignOutAsync("Cookies");
    // Signout oidc
    await HttpContext.SignOutAsync("oidc");
    

    【讨论】:

    • @ruard-van-elburg 非常感谢!网址现在有效。但是退出问题仍然没有解决,因为我使用 JWT 令牌,不幸的是,我知道常见的退出方法不适用于他们
    • @d_f Lady 在反向通道上调用 endsession,因为她使用 React -> backend -> IdentityServer 架构并自行配置 IdentityServer 并调用 /connect/token 或 connect/endpoint 等请求,这对我来说是正确的方式我知道:))
    • @Melianessa 根本不适合通过反向渠道工作:注销的想法是清除 Idp 的身份验证 cookie + 通知在当前会话期间登录的所有客户端——所有这些操作都需要访问到浏览器,因此必须通过你的前面。这是默认方式。你可以实现你自己的。但在这种情况下,您需要开始考虑将 idsrv 会话保留在某个自定义位置,而不是 cookie。你真的需要那个吗?难以置信
    • @Melianessa 当您根本不需要 idsrv 中的会话时(只有一个应用程序,不需要 SSO),使用资源所有者密码流,注销时,只需清除您在本地保存的令牌应用程序,不要触摸 IdSrv。那应该工作。但在那种情况下,你不应该需要 idsrv ......再次不清楚
    • @Melianessa jwt 在过期之前不能失效——这是设计使然。这就是为什么调用 endsession 端点不会帮助你的原因。那是关于会话、cookie 和持久授权,而不是关于某人在某处持久存在的 jwts。你可以用它做些什么 - 设置尽可能短的ttl。并在需要时使用刷新令牌获取新的承载。并在注销时删除该刷新令牌。
    猜你喜欢
    • 2020-07-29
    • 2018-08-17
    • 2015-03-30
    • 2017-11-15
    • 2019-11-23
    • 1970-01-01
    • 2020-11-20
    • 2018-06-12
    • 2011-04-10
    相关资源
    最近更新 更多