.net-core - AWS Cognito .NET Core 3.1 集成
问题描述
一般来说,我对 .NET 比较陌生,我正在尝试使用 AWS Cognito 为我的 API 创建一个简单的身份验证。我已经按照在线指南进行操作,但似乎没有一个对我有用。这就是我配置 Startup.cs 的方式:
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddControllersWithViews();
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(options =>
{
options.ResponseType = Configuration["Authentication:Cognito:ResponseType"];
options.MetadataAddress = Configuration["Authentication:Cognito:MetadataAddress"];
options.ClientId = Configuration["Authentication:Cognito:ClientId"];
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
我已经正确填写了我的配置。当我通过保护一个页面时,[Authorize]
我被重定向到 AWS Cognito 登录,然后我被重定向到https://localhost:5001/signin-oidc?code=...
我得到一个空白页面的地方,并且控制台中出现错误说明:
fail: Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler[52]
Message contains error: 'invalid_client', error_description: 'error_description is null', error_uri: 'error_uri is null', status code '400'.
fail: Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler[17]
Exception occurred while processing message.
Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectProtocolException: Message contains error: 'invalid_client', error_description: 'error_description is null', error_uri: 'error_uri is null'.
at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.RedeemAuthorizationCodeAsync(OpenIdConnectMessage tokenEndpointRequest)
at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleRemoteAuthenticateAsync()
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HLTB2GSVEPB1", Request id "0HLTB2GSVEPB1:00000013": An unhandled exception was thrown by the application.
System.Exception: An error was encountered while handling the remote login.
---> Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectProtocolException: Message contains error: 'invalid_client', error_description: 'error_description is null', error_uri: 'error_uri is null'.
at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.RedeemAuthorizationCodeAsync(OpenIdConnectMessage tokenEndpointRequest)
at Microsoft.AspNetCore.Authentication.OpenIdConnect.OpenIdConnectHandler.HandleRemoteAuthenticateAsync()
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Authentication.RemoteAuthenticationHandler`1.HandleRequestAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)
我试图确保它在 AWS 端没有错,并且我已经能够看到代码和状态被发送回应用程序。
解决方案
推荐阅读
- go - cgo 似乎断货了 go on stock cygwin - 是否支持?
- tcp - 客户端和服务器在通信时可以使用不同的拥塞算法吗?
- docker - 无法创建卷
- java - 使用缓存时的不可变类
- typescript - 为 Vue 道具创建类型的正确方法是什么
- c - 出现错误“未定义对‘LZ4_compressBound’和‘LZ4_compress_default’的引用
- json - 在这种情况下如何从 json 解码?
- php - 使用 Ajax 从 modal 向 mySQL 发送数据
- python - 如何以只读方式将表中的数据插入组合框?
- java - 有人可以向我解释这个快速排序算法吗?