如何使用结束会话端点 IdentityServer4 注销?

Mel*_*ssa 5 c# asp.net identityserver4

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

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

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

Invalid URI: The Uri scheme is too long. 
Run Code Online (Sandbox Code Playgroud)

为了解决这个问题,我尝试将参数发送到字符串中,如下所示:

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

还要在 Program.cs 中添加MaxRequestLineSize,如下所示:

UseKestrel(options =>
{
  options.Limits.MaxRequestLineSize = 20480;
})
Run Code Online (Sandbox Code Playgroud)

也尝试过这种方式:/sf/answers/2272023211/但对我来说没有任何作用。

我尝试通过邮递员发送此请求。请求已发送

http://localhost:5000/connect/endsession?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}
Run Code Online (Sandbox Code Playgroud)

但在 IClientStore 接口的 FindClientByIdAsync 方法中 clientId 参数如下所示: 在此输入图像描述

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

小智 3

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

?id_token_hint={idTokenHint}&post_logout_redirect_uri={postLogoutRedirectUri}
Run Code Online (Sandbox Code Playgroud)

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

对 uri 进行编码:

var encodedUri = System.Web.HttpUtility.UrlEncode(postLogoutRedirectUri);
Run Code Online (Sandbox Code Playgroud)

并使用它作为参数:

?id_token_hint={idTokenHint}&post_logout_redirect_uri={encodedUri}
Run Code Online (Sandbox Code Playgroud)

虽然这可能会解决问题,但为什么不使用提供的方法来注销呢?例如:

//using Microsoft.AspNetCore.Authentication;
//using Microsoft.AspNetCore.Mvc;

// Remove cookie
await HttpContext.SignOutAsync("Cookies");
// Signout oidc
await HttpContext.SignOutAsync("oidc");
Run Code Online (Sandbox Code Playgroud)

  • @Melianessa jwt 在过期之前无法失效——这是设计使然。这就是为什么调用 endsession 端点对您没有帮助。这是关于会话、cookie 和持久授权,而不是关于某人在某处持久存在的 jwts。你能做的就是设置尽可能短的 ttl。并在需要时使用刷新令牌来获取新的承载。并在注销时删除该刷新令牌。 (2认同)