找不到IdentityServer4 Testserver [英] IdentityServer4 Testserver could not found

查看:110
本文介绍了找不到IdentityServer4 Testserver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个测试,以使用Microsoft.AspNetCore.TestHost从identity server4获取令牌

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");

然后disco.Error出现以下错误

Then disco.Error comes up with the following error

连接到错误 http://localhost:5001/.well-known/openid-configuration :一个错误 发送请求时发生.

Error connecting to http://localhost:5001/.well-known/openid-configuration: An error occurred while sending the request.

我想念什么?

推荐答案

发现客户端显然正在对该实际地址进行外部调用.您希望它调用恰好激活" InMemory的测试服务器.

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.

看看这些测试此处用于测试发现文档的IdentityServer4.

Take a look at these tests here for IdentityServer4 that tests the discovery document.

要回答您的问题,尽管您需要对DiscoveryClient使用重载方法之一,该方法采用了将对InMemory测试服务器进行正确调用"的处理程序.下面是如何完成此操作的示例.

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();

如果您要像这样自己做一些测试,我也强烈建议您复习IdentityServer4集成测试.

Also I highly recommend going over the IdentityServer4 integration tests if youre going to be doing some of your own tests like this.

这篇关于找不到IdentityServer4 Testserver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆