如何使用结束会话终结点IdentityServer4登出?

时间:2019-05-23 13:07:37

标签: c# asp.net identityserver4

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

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

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

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但对我没有任何帮助。

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

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

但是在IClientStore接口的FindClientByIdAsync方法中,clientId参数看起来像这样: enter image description here

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

1 个答案:

答案 0 :(得分:3)

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

?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");