带有 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*
环境
- .NET Core 应用程序
- AWS Cognito 用户池
- 使用 .NET Core 的自定义 SSO 应用程序
- 打开 ID Connect
背景
我们在用户计算机上有多个 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
已浏览教程
- https://dzone.com/articles/identity-as-a-service-idaas-aws-cognito-and-aspnet
- Can i use AWS cognito to provide a open id connect endpoint?
- https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html(连同令牌和登录名)
- https://github.com/IdentityModel/IdentityModel.OidcClient/issues/121
Fiddler 响应
终于在添加单元测试支持后,我意识到由于数据结构不正确,ClientSecret 没有通过。添加后,应用程序正常运行。
环境
- .NET Core 应用程序
- AWS Cognito 用户池
- 使用 .NET Core 的自定义 SSO 应用程序
- 打开 ID Connect
背景
我们在用户计算机上有多个 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 中跟踪时使用
- OAuth 流程:
Authorization Code Grant
- 范围:
openid profile email
authorise
和 login
端点时会出现这种情况
已浏览教程
- https://dzone.com/articles/identity-as-a-service-idaas-aws-cognito-and-aspnet
- Can i use AWS cognito to provide a open id connect endpoint?
- https://docs.aws.amazon.com/cognito/latest/developerguide/authorization-endpoint.html(连同令牌和登录名)
- https://github.com/IdentityModel/IdentityModel.OidcClient/issues/121
Fiddler 响应
终于在添加单元测试支持后,我意识到由于数据结构不正确,ClientSecret 没有通过。添加后,应用程序正常运行。