问题描述
我有一个基于AspNet.Security.OpenIdConnect.Server
的OpenIdDict
身份验证服务器.该设置将按预期工作.
I have an OpenIdDict
authentication server which is based on AspNet.Security.OpenIdConnect.Server
. The setup works as expected.
现在要进行一些流程集成;跨越整个后端体系结构的系统测试我将使用TestServer
类.
Now to do some in process integration;system tests which span the whole backend architecture I use the TestServer
class.
- 测试代码覆盖率最高,工作量最少
- 已决定不进行单元测试...(他们说太多的工作)
- 真正的集成测试,所涉及的代码要少得多,而当我想要获得良好的覆盖率时,这也可以看作是很多工作
- 该测试基于使用领域语言方法构建的框架,这意味着我可以为我们当前的Web api测试,Selenium Web ui测试,Selenium负载测试和wpf ui测试描述相同的功能. /li>
- Most test code coverage with least amount of work
- It has been decided to not do unit tests... (too much work they say)
- Real integration tests which span much less code where also seen as to much work when I want to achieve a good coverage
- The test are based on an framework that is build using a domain language approach which means I can describe functionality the same for our current web api tests, for selenium web ui tests, for selenium load tests and for wpf ui testing.
- AllowInsecureHttp = true,
- RequireHttpsMetadata = false
当我调用资源服务器的Web api端点时,授权要加载
http://localhost/.well-known/openid-configuration
,但失败.When I call an web api endpoint of my ressource server the authorization wants to load
http://localhost/.well-known/openid-configuration
but fails.这是我用于测试环境的OpenIdConnectSettings:
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?
推荐答案
TestServer
需要注意的重要一点是,一切都发生在内存中:没有打开套接字来处理您的应用程序可能发送的HTTP请求.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.不幸的是,OpenID Connect客户端中间件(内部使用
HttpClient
)无法得知,并试图向OpenIddict发送真实的" HTTP请求以检索发现文档.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.要变通解决此问题,建议的方法是替换OIDC中间件使用的默认反向通道处理程序,以使用
TestServer.CreateHandler()
提供的内存中处理程序: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() });
注意:相同的方法也适用于JWT承载中间件和aspnet-contrib自省中间件.
Note: the same approach also applies to the JWT bearer middleware and the aspnet-contrib introspection middleware.
这篇关于用TestServer托管ASOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!