IdentityServer4 Testserver could not found
Asked Answered
K

1

7

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?

Kreit answered 26/1, 2017 at 17:34 Comment(0)
F
9

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.

Fabrianne answered 26/1, 2017 at 17:59 Comment(5)
this works like a charm, Thanks. But the link is broken :(Kreit
sorry, fixed link :)Fabrianne
link is broken again :(Impetuous
@LuisPalacios fixed the broken link to a perma link. Hope this helpsFabrianne
It did!, Thank you!Impetuous

© 2022 - 2024 — McMap. All rights reserved.