带有 OpenID 的 AWS Cognito 因 .NET Core 失败给出错误 *Error redeeming code: invalid_client*

AWS Cognito with OpenID failing with .NET Core gives error of *Error redeeming code: invalid_client*

环境

背景

我们在用户计算机上有多个 windows 应用程序(WPF 和 WinForms)运行。用户在 AWS Cognito 的自定义用户池中进行管理。与其他服务的联合存在,但超出了这项工作的范围。 SSO 应用程序显示一个登录屏幕,用户在其中输入他们的凭据并通过 Cognito 进行身份验证,取回授权代码。登录机制是通过 OpenID Connect。

问题陈述

用户通过 Cognito 成功验证,但 OIDC 登录调用给出如下错误消息:-

Error redeeming code: invalid_client / no description

从 Cognito 收到的响应如下,结果代码为成功:-

http://localhost/myBusinessApp?code=5a0507ed-5230-408d-&state=KHfMkdZphxxxxxxxxxx

**代码**

授权应用程序

            var options = new OidcClientOptions()
        {
            Authority = _authenticationConfig.Value.Authority,
            ClientId = _authenticationConfig.Value.ClientId,
            ClientSecret = _authenticationConfig.Value.ClientSecret,  
            Scope = _authenticationConfig.Value.Scope,
            RedirectUri = _authenticationConfig.Value.RedirectUri,
            Browser = new CEFBrowser(),
            Policy = new Policy()
            {
                Discovery = new DiscoveryPolicy()
                {
                    ValidateIssuerName = false ,  /* added for Cognito only */
                    ValidateEndpoints = false /* added for Cognito only */    
                }
            }, 
            Flow = OidcClientOptions.AuthenticationFlow.AuthorizationCode  /* default, but just for clarity */
        };               
        
        _oidcClient = new IdentityModel.OidcClient.OidcClient(options);

        LoginResult result;
        try
        {
            result = await _oidcClient.LoginAsync();
        }
        catch (Exception ex)
        {
            Message.Text = $"Unexpected Error: {ex.Message}";
            return;
        }

        if (result.IsError)
               // Log error (always happen)

浏览器调用 Cognito

这总是成功重定向到配置的 RedirectUrl

       webBrowser.LoadingStateChanged += (s, e) =>
       {
           var url = e.Browser.MainFrame.Url;
           if (url.StartsWith(_options.EndUrl))
           {
               result = new BrowserResult()
               {
                   ResultType = BrowserResultType.Success,
                   Response = url
               };

               signal.Release();

               webBrowser.Dispatcher.Invoke(() => window.Close());
           }
       };

认知配置

已浏览教程

Fiddler 响应

终于在添加单元测试支持后,我意识到由于数据结构不正确,ClientSecret 没有通过。添加后,应用程序正常运行。