我正在开发 .Net API (netcoreapp3.1),并使用 IONOS 进行托管 (.net Framework 4.x)。
用户基于 firebase 身份验证机制连接到我的前端应用程序,我将 JWT 令牌发送到我的 API 以验证令牌,以便访问资源(路由)。
令牌验证在本地工作得很好,但是当我在 IONOS 上发布我的 API 后,我遇到了这个异常。
System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'https://securetoken.google.com/XXXXXXXX/.well-known/openid-configuration'.
---> System.IO.IOException: IDX20804: Unable to retrieve document from: 'https://securetoken.google.com/XXXXXXXX/.well-known/openid-configuration'.
---> System.Net.Http.HttpRequestException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
---> System.Net.Sockets.SocketException (10060): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectAsync(HttpRequestMessage request, Boolean allowHttp2, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.CreateHttp11ConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.GetHttpConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)
--- End of inner exception stack trace ---
at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)
at Microsoft.IdentityModel.Protocols.OpenIdConnect.OpenIdConnectConfigurationRetriever.GetAsync(String address, IDocumentRetriever retriever, CancellationToken cancel)
at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
--- End of inner exception stack trace ---
at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
更多信息:
我在 API 中激活了
IdentityModelEventSource.ShowPII = true;
获取有关异常的更多信息
我验证了我的所有配置都使用“https”进行令牌验证
API启动代码:
string firebaseCredentialPath = $"firebaseCredential.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json";
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile(firebaseCredentialPath)
});
string firebaseAppId = Configuration["Firebase:FirebaseAppId"];
string firebaseAuthorityUrl = string.Format(Configuration["Firebase:FirebaseAuthorityUrl"], firebaseAppId);
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
options.Authority = firebaseAuthorityUrl;
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = firebaseAuthorityUrl,
ValidAudience = firebaseAppId,
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
};
});
我的应用程序设置配置
“Firebase”:{ "FirebaseAuthorityUrl": "https://securetoken.google.com/{0}", “FirebaseAppId”:“XXXXXXXX” }
我也遇到了同样的问题,我需要在Ocelot微服务中实现这个配置。
您可能已经使用过本指南:https://dominique-k.medium.com/using-firebase-jwt-tokens-in-asp-net-core-net-5-834c43d4aa00
但我注意到我的问题出在
https://securetoken.google.com/FIREBASE-APP-ID
部分,我发现我需要采用 projectId
而不是 appId
,您可以在 项目配置 中找到并且工作正常:
我已经意识到感谢这个指南
这是启动类方法的重定向问题
configure => app.UseHttpsRedirection();
。
我用这个解决方案更改了这一行:无法将 Ionos 微软托管网站自动重定向到 https
这是 IONOS 托管将 http 请求重定向到 https 的特定配置。