问题
I am writing a test to get a token from identity server4 using Microsoft.AspNetCore.TestHost
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");
Then disco.Error comes up with the following error
Error connecting to http://localhost:5001/.well-known/openid-configuration: An error occurred while sending the request.
What am i missing?
回答1:
The discovery client is obviously doing an external call to that actual address. You want it to call the test server that happens to "live" InMemory.
Take a look at these tests here for IdentityServer4 that tests the discovery document.
To answer your question though you need to use one of the overloaded methods for the DiscoveryClient
that takes in a handler that would make the correct "call" to your InMemory test server. Below is an example of how this could be done.
var server = new TestServer(hostBuilder);
var handler = server.CreateHandler();
var discoveryClient = new DiscoveryClient("http://localhost:5000", handler);
var discoveryDocument = await discoveryClient.GetAsync();
Also I highly recommend going over the IdentityServer4 integration tests if youre going to be doing some of your own tests like this.
来源:https://stackoverflow.com/questions/41879515/identityserver4-testserver-could-not-found