简体   繁体   English

找不到 IdentityServer4 测试服务器

[英]IdentityServer4 Testserver could not found

I am writing a test to get a token from identity server4 using Microsoft.AspNetCore.TestHost我正在编写测试以使用 Microsoft.AspNetCore.TestHost 从identity server4获取令牌

   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然后disco.Error 出现以下错误

Error connecting to http://localhost:5001/.well-known/openid-configuration : An error occurred while sending the request.连接到http://localhost:5001/.well-known/openid-configuration 时出错:发送请求时出错。

What am i missing?我错过了什么?

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.您希望它调用恰好“活动”InMemory 的测试服务器。

Take a look at these tests here for IdentityServer4 that tests the discovery document.此处查看用于测试发现文档的 IdentityServer4 的这些测试。

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.要回答您的问题,但您需要使用DiscoveryClient的重载方法之一,该方法接受一个处理程序,该处理程序将对您的 InMemory 测试服务器进行正确的“调用”。 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.如果您要进行一些这样的自己的测试,我也强烈建议您查看 IdentityServer4 集成测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM