c# - 带有 OpenID 的 AWS Cognito 使用 .NET Core 失败时会出现*错误兑换代码:invalid_client*
问题描述
环境
- .NET Core 应用
- AWS Cognito 用户池
- 使用 .NET Core 的自定义 SSO 应用
- 打开 ID 连接
背景
我们在用户机器上运行了多个 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());
}
};
认知配置
- 具有示例用户的用户池
- 权威 (
https://console.aws.amazon.com/cognito/users/?region=us-east-1#/pool/us-east-1_spXXXX/users?_k=m7yyyy
) - 域名设置(Cognito URL 的自定义域前缀)。在 Fiddler 中跟踪时使用
authorise
和端点时会出现这种情况login
- OAuth 流程:
Authorization Code Grant
- 范围:
openid profile email
已经探索的教程
- AWS Cognito 用户池和 OpenId
- https://dzone.com/articles/identity-as-a-service-idaas-aws-cognito-and-aspnet
- 我可以使用 AWS cognito 来提供开放 id 连接端点吗?
- https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html(连同令牌和登录)
- https://github.com/IdentityModel/IdentityModel.OidcClient/issues/121
提琴手响应
解决方案
最后在添加单元测试支持后,我意识到由于数据结构不正确,ClientSecret 没有通过。添加后,该应用程序正常运行。
推荐阅读
- java - 因为当我导入这个 camel-jaxb 库时,所有 marshall 的工作方式都不同
- react-native - 如何在不使用 appium 构建 apk 的情况下测试 Expo 应用程序?
- ios - 如何在不重新加载 TableView 的情况下将多个单元格添加到 TableView?迅速
- python - 达到指定值时将新迭代附加到变量的 Pyomo 约束
- spring - 文件的 Spring 事务管理
- django - 如何使用 Django ForeignKey 将模型的列链接到另一个
- reactjs - ReactJS - 如何在我们拥有可用状态的数据时填充单个实体数据
- node.js - LESS 安装 - Linux 上不存在节点文件
- java - DirectionsApiRequest 和 apiRequest.setCallback 的问题
- python - 自动更正的回文 Python 代码