1

我是 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 或状态。

4

1 回答 1

0

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

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

它现在正在工作。

于 2022-03-01T22:58:14.063 回答