参数计数不匹配 CreateEndSessionUrl - IdentityModel

发布于 2025-01-10 19:01:42 字数 2477 浏览 2 评论 0原文

我是 C# 新手。我正在将 .NET IdentityModel 与 AWS Cognito 用户池结合使用,并尝试让注销正常工作。 CreateEndSessionUrl 设置 post_logout_redirect_uri 但 Cognito 需要 logout_uri。我尝试使用 extra 参数,但出现参数计数不匹配的情况。

这是我的代码:

StringDictionary cognitoParameters = new StringDictionary();
cognitoParameters.Add("client_id", OAuthConfiguration.ClientId);
cognitoParameters.Add("logout_uri", OAuthConfiguration.EndsessionEndpointPath);

var endsessionEndpoint = OAuthConfiguration.Authority.TrimEnd('/') + "/" + OAuthConfiguration.EndsessionEndpointPath;
var requestUrl = new RequestUrl(endsessionEndpoint);
var endSessionUrl = requestUrl.CreateEndSessionUrl(
   idTokenHint: HttpContext.Current.GetToken(OidcConstants.ResponseTypes.IdToken),
   postLogoutRedirectUri: OAuthConfiguration.Host,
   state: null,
   extra: cognitoParameters
);

CreateEndSessionUrl 文档 说“额外的参数可以可以是字符串字典,也可以是具有属性的任意其他类型。在这两种情况下,值都将被序列化为键/值。”我假设我以某种方式错误地创建了字符串字典。

我收到的错误是:

Message: Parameter count mismatch.

Exception type: System.Reflection.TargetParameterCountException
Stack trace:
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at IdentityModel.Internal.ValuesHelper.ObjectToDictionary(Object values)
at IdentityModel.Client.RequestUrlExtensions.CreateEndSessionUrl(RequestUrl request, String idTokenHint, String postLogoutRedirectUri, String state, Object extra)
at Indice.Kentico.Oidc.EndSessionOidcHandler.EndSession()
at Indice.Kentico.Oidc.EndSessionOidcHandler.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

有人可以帮助我了解如何正确格式化并包含 extra 参数吗?我实际上不需要 idTokenHint、postLogoutRedirectUri,或说明是否可以排除它们。

I'm new to C#. I'm using .NET IdentityModel with AWS Cognito User Pools and attempting to get logout to work. CreateEndSessionUrl sets post_logout_redirect_uri but Cognito requires logout_uri. I'm attempting to use the extra parameter but getting a Parameter Count Mismatch.

Here is my code:

StringDictionary cognitoParameters = new StringDictionary();
cognitoParameters.Add("client_id", OAuthConfiguration.ClientId);
cognitoParameters.Add("logout_uri", OAuthConfiguration.EndsessionEndpointPath);

var endsessionEndpoint = OAuthConfiguration.Authority.TrimEnd('/') + "/" + OAuthConfiguration.EndsessionEndpointPath;
var requestUrl = new RequestUrl(endsessionEndpoint);
var endSessionUrl = requestUrl.CreateEndSessionUrl(
   idTokenHint: HttpContext.Current.GetToken(OidcConstants.ResponseTypes.IdToken),
   postLogoutRedirectUri: OAuthConfiguration.Host,
   state: null,
   extra: cognitoParameters
);

The CreateEndSessionUrl documentation says "The extra parameter can either be a string dictionary or an arbitrary other type with properties. In both cases the values will be serialized as keys/values." I assume I'm creating the string dictionary incorrectly somehow.

The error I get is:

Message: Parameter count mismatch.

Exception type: System.Reflection.TargetParameterCountException
Stack trace:
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
at IdentityModel.Internal.ValuesHelper.ObjectToDictionary(Object values)
at IdentityModel.Client.RequestUrlExtensions.CreateEndSessionUrl(RequestUrl request, String idTokenHint, String postLogoutRedirectUri, String state, Object extra)
at Indice.Kentico.Oidc.EndSessionOidcHandler.EndSession()
at Indice.Kentico.Oidc.EndSessionOidcHandler.ProcessRequest(HttpContext context)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

Can someone help me understand how to properly format and include the extra parameter? I actually don't need the idTokenHint, postLogoutRedirectUri, or state if they can be excluded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆依然 2025-01-17 19:01:45

我发现我需要创建一个字典而不是 StringDictionary:

IDictionary cognitoParameters = new Dictionary<string,string>() {
  { "client_id", OAuthConfiguration.ClientId },
  { "logout_uri", OAuthConfiguration.Host.TrimEnd('/') + "/SignOut.ashx" }
};

它现在正在工作。

I figured out that I needed to create a Dictionary instead of a StringDictionary:

IDictionary cognitoParameters = new Dictionary<string,string>() {
  { "client_id", OAuthConfiguration.ClientId },
  { "logout_uri", OAuthConfiguration.Host.TrimEnd('/') + "/SignOut.ashx" }
};

It is working now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文