c# - ConfigurationManager.GetConfigurationAsync 无法在 TestServer 中检索身份配置
问题描述
我正在使用Microsoft.AspNetCore.TestHost
版本 2.2.0 和Microsoft.NET.Test.Sdk
版本 15.9.0。我将 IdentityServer4 用于 OAuth2 提供程序以进行 Jwt Bearer 身份验证。使用真正的 Web 服务器部署时,该设置运行良好。但是,在使用TestServer
.
我已经注册了HttpMessageHandler
:
private void InitializeServices<TStartup>(IServiceCollection services)
{
var startupAssembly = typeof(TStartup).GetTypeInfo().Assembly;
var applicationPartManager = new ApplicationPartManager();
applicationPartManager.ApplicationParts.Add(new AssemblyPart(startupAssembly));
applicationPartManager.FeatureProviders.Add(new ControllerFeatureProvider());
applicationPartManager.FeatureProviders.Add(new ViewComponentFeatureProvider());
services.AddSingleton(applicationPartManager);
services.PostConfigure<IdentityServerAuthenticationOptions>(options =>
{
options.IntrospectionDiscoveryHandler = this.server.CreateHandler();
options.IntrospectionBackChannelHandler = this.server.CreateHandler();
options.JwtBackChannelHandler = this.server.CreateHandler();
});
}
this.server
我的测试服务器在哪里。但是,当我运行集成测试时,出现以下错误。
Result StackTrace:
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 IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler.HandleAuthenticateAsync() in C:\local\identity\server4\AccessTokenValidation\src\IdentityServer4.AccessTokenValidation\IdentityServerAuthenticationHandler.cs:line 61
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 Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
at IdentityServer4.Hosting.BaseUrlMiddleware.Invoke(HttpContext context) in C:\local\identity\server4\IdentityServer4\src\IdentityServer4\Hosting\BaseUrlMiddleware.cs:line 36
at Microsoft.AspNetCore.Cors.Infrastructure.CorsMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.TestHost.HttpContextBuilder.<>c__DisplayClass10_0.<<SendAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.TestHost.ClientHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at xxx.ClientControllerTest.CreateNewClientAsync() in xxx.ClientControllerTest.cs:line 167
at xxx.ClientControllerTest.CreateNewClientTest() in xxx\ControllerTests\ClientControllerTest.cs:line 47
--- End of stack trace from previous location where exception was thrown ---
----- Inner 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)
----- Inner Stack Trace -----
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.CreateConnectionAsync(HttpRequestMessage request, CancellationToken cancellationToken)
at System.Threading.Tasks.ValueTask`1.get_Result()
at System.Net.Http.HttpConnectionPool.WaitForCreatedConnectionAsync(ValueTask`1 creationTask)
at System.Threading.Tasks.ValueTask`1.get_Result()
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.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)
at Microsoft.IdentityModel.Protocols.HttpDocumentRetriever.GetDocumentAsync(String address, CancellationToken cancel)
----- Inner Stack Trace -----
at System.Net.Http.ConnectHelper.ConnectAsync(String host, Int32 port, CancellationToken cancellationToken)
Result Message:
System.InvalidOperationException : IDX20803: Unable to obtain configuration from: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'.
---- System.IO.IOException : IDX20804: Unable to retrieve document from: '[PII is hidden by default. Set the 'ShowPII' flag in IdentityModelEventSource.cs to true to reveal it.]'.
-------- System.Net.Http.HttpRequestException : No connection could be made because the target machine actively refused it
------------ System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it
解决方案
推荐阅读
- kotlin - 调用解构声明初始化程序的“component1()”函数需要不可为空的值
- java - 回滚正在处理事务,但无论如何都会因 SQLNonTransientConnectionException 而崩溃
- solr - Solr 构面组查询过滤
- linux - 为什么 SIGHUP 在 Alpine Docker 容器中的 busybox sh 上不起作用?
- python-3.x - 根据条件在熊猫数据框中创建一列
- php - 已解决 - pdo mssql fetchColumn()
- java - Java:如何使某些 JSON 字段不被打印
- html - 尝试实现简单的 CSS 布局时元素未正确对齐
- node.js - 如何修复我的应用程序中最近出现的 node.js 错误?
- drupal-8 - 如何在 drupal 8 规则中发送 html 邮件