【发布时间】:2019-03-27 23:14:01
【问题描述】:
更新:更正证书问题后,我现在在测试中收到 500 响应,其中包含以下消息:
InvalidOperationException:IDX20803:无法从“http://localhost/.well-known/openid-configuration”获取配置。
这似乎与这个问题相似:https://github.com/IdentityServer/IdentityServer4/issues/685;但是,我无法从我的测试中想出一种方法来设置反向通道客户端或处理程序——这似乎是先有鸡还是先有蛋的情况。
已通过使用真实证书/.pfx 文件解决了此问题。这导致了上述问题。
我正在使用WebApplicationFactory 对我的 API 进行集成测试,并且我认为我已经涵盖了正确配置 http 客户端的所有基础。在我的 API 中调用操作时出现错误。
这是使用令牌对 api 执行 get 时的错误:
WWW-Authenticate: Bearer error="invalid_token", error_description="找不到签名密钥"
这是一个演示这个问题的简单测试类:
public class EntityControllerShould : IClassFixture<WebApplicationFactory<Startup>>
{
private readonly WebApplicationFactory<Startup> _factory;
public EntityControllerShould(WebApplicationFactory<Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task ReturnListOfEntities_ForGet()
{
// arrange
_factory.CreateClient();
var handler = _factory.Server.CreateHandler();
var client = new HttpClient(handler) { BaseAddress = new System.Uri("http://localhost/") };
// discover endpoints from metadata
var dc = new DiscoveryClient(client.BaseAddress.ToString(), handler);
var disco = await dc.GetAsync();
if (disco.IsError)
{
Assert.True(false);
}
// request token
var tokenClient = new TokenClient(disco.TokenEndpoint, "api_client", "secret", handler);
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("api1");
if (tokenResponse.IsError)
{
Assert.True(false);
}
client.SetBearerToken(tokenResponse.AccessToken);
// act
var response = await client.GetAsync("api/entity/?id=123");
// response code is 401 with the above quoted error in the header
response.EnsureSuccessStatusCode();
var responseString = await response.Content.ReadAsStringAsync();
// assert
Assert.NotNull(responseString);
}
}
从 API 项目中的 Startup.cs 截取:
services.AddIdentityServer()
.AddSigningCredential(myCertificate)
.AddSigningCredential()
.AddClientStore<CustomClientStore>()
.AddInMemoryIdentityResources(IdentityServerConfig.IdentityResources)
.AddInMemoryApiResources(IdentityServerConfig.Apis);
- 我将 IdentityServer4 和 API 托管在同一个项目中。
- 当我通过浏览器手动执行集成测试时,它工作得很好
- 我找到了this which seems very similar
在我不是 xunit 测试的上下文中运行时我需要考虑什么?
【问题讨论】:
标签: c# .net oauth identityserver4 openid-connect