我正在编写测试以使用Microsoft.AspNetCore.TestHost从身份服务器4获取令牌
var hostBuilder = new WebHostBuilder()
.ConfigureServices(services =>
{
services.AddIdentityServer()
.AddTemporarySigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers())
;
})
.Configure(app =>
{
app.UseIdentityServer();
});
var server = new TestServer(hostBuilder);
var client = server.CreateClient();
client.BaseAddress = new Uri("http://localhost:5000");
var disco = await DiscoveryClient.GetAsync("http://localhost:5000");
然后disco.Error出现以下错误
连接时出错 http://localhost:5001/.well-known/openid-configuration:错误 发送请求时发生。
我缺少什么?
答案 0 :(得分:6)
发现客户端显然正在对该实际地址进行外部调用。您希望它调用恰好" live" InMemory。
查看测试发现文档的IdentityServer4的这些测试here。
要回答你的问题,你需要使用DiscoveryClient
的一个重载方法,它接受一个能够正确调用"调用"到您的InMemory测试服务器。下面是一个如何做到这一点的例子。
var server = new TestServer(hostBuilder);
var handler = server.CreateHandler();
var discoveryClient = new DiscoveryClient("http://localhost:5000", handler);
var discoveryDocument = await discoveryClient.GetAsync();
如果你要做一些像这样的自己的测试,我强烈建议你去看一下IdentityServer4集成测试。