Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IdentityServer4 Testserver could not found

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?

like image 792
MJK Avatar asked Jan 26 '17 17:01

MJK


1 Answers

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.

like image 57
Lutando Avatar answered Sep 21 '22 23:09

Lutando