【发布时间】:2022-07-12 21:34:27
【问题描述】:
在我的应用中,我使用以下代码添加:
authBuilder
.AddJwtBearer("MyCustomScheme", options =>
{
options.Authority = "https://auth.example.com";
options.AutomaticRefreshInterval = new TimeSpan(24, 0, 0);
});
在我的集成测试项目中,我有以下代码:
public class TestAppFactory : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
// tried also with .ConfigureTestServices
builder.ConfigureServices(services =>
{
services.PostConfigure<JwtBearerOptions>("MyCustomScheme", options =>
{
// this is executed and options.Authority is auth.example.com here
options.MetadataAddress = "https://inmemory.microsoft.com/common/.well-known/openid-configuration";
options.Authority = "https://inmemory.microsoft.com/common/.well-known/openid-configuration";
options.BackchannelHttpHandler = new MockBackchannel();
});
});
}
}
而 MockBackendChannel 看起来像这样:
public class MockBackchannel : HttpMessageHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.RequestUri.AbsoluteUri.Equals("https://inmemory.microsoft.com/common/.well-known/openid-configuration"))
{
return await EmbeddedResourceReader.GetOpenIdConfigurationAsResponseMessage("microsoft-openid-config.json");
}
if (request.RequestUri.AbsoluteUri.Equals("https://inmemory.microsoft.com/common/discovery/keys"))
{
return await EmbeddedResourceReader.GetOpenIdConfigurationAsResponseMessage("microsoft-wellknown-keys.json");
}
throw new NotImplementedException();
}
}
我检查了services.PostConfigure<JwtBearerOptions> 是否在应用程序中设置的选项上正确调用,但是当我在集成测试中调用授权 API 时(使用Application.CreateClient() 创建的客户端)我收到以下异常:
System.InvalidOperationException: IDX20803: Unable to obtain configuration from: 'System.String'.
---> System.IO.IOException: IDX20804: Unable to retrieve document from: 'System.String'.
---> System.Net.Http.HttpRequestException: nodename nor servname provided, or not known (auth.example.com:443)
---> System.Net.Sockets.SocketException (0xFFFDFFFF): nodename nor servname provided, or not known
所以由于某种原因后配置对方案没有影响,它仍在尝试调用 auth.example.com。我怎样才能做到这一点?
【问题讨论】:
标签: .net-core