NtFreX
NtFreX

Reputation: 11367

Hosting ASOS with TestServer

I have an OpenIdDict authentication server which is based on AspNet.Security.OpenIdConnect.Server. The setup works as expected.

Now to do some in process integration;system tests which span the whole backend architecture I use the TestServer class.

Why I test like this is another question

When I call an web api endpoint of my ressource server the authorization wants to load http://localhost/.well-known/openid-configuration but fails.

{"IDX10803: Unable to obtain configuration from: 'http://localhost/.well-known/openid-configuration'."})

This are the OpenIdConnectSettings I use for the Testing Environment:

Can I get the server to emit the configuration or can I provide the configuration in an other way?

Upvotes: 4

Views: 941

Answers (1)

Kévin Chalet
Kévin Chalet

Reputation: 42100

What's important to note with TestServer is that everything happens in memory: no socket is open to handle the HTTP requests your application might send.

Unfortunately, the OpenID Connect client middleware (that uses HttpClient internally) has no way to know that and tries to send a "real" HTTP request to OpenIddict to retrieve the discovery document.

To work around this issue, the recommended approach is to replace the default backchannel handler used by the OIDC middleware to use the in-memory handler provided by TestServer.CreateHandler():

app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
    Authority = "http://localhost:54540/",
    RequireHttpsMetadata = false,
    ClientId = "myClient",
    ClientSecret = "secret_secret_secret",
    BackchannelHttpHandler = server.CreateHandler()
});

Note: the same approach also applies to the JWT bearer middleware and the aspnet-contrib introspection middleware.

Upvotes: 4

Related Questions