【问题标题】:How do you configure ASP.Net TestHost to work with OpenId Connect?如何配置 ASP.Net TestHost 以使用 OpenId Connect?
【发布时间】:2017-03-06 15:07:36
【问题描述】:

我有一个 ASP.Net Core 应用程序配置为发布和验证 JWT 不记名令牌。当网站托管在 Kestrel 中时,客户端能够成功检索不记名令牌并使用令牌进行身份验证。

我还有一套使用 Microsoft.AspNetCore.TestHost.TestServer 的集成测试。在添加身份验证之前,测试能够成功地向应用程序发出请求。添加身份验证后,我开始收到与访问开放 ID 配置有关的错误。我看到的具体例外是:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://  
fail: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerMiddleware[3]
      Exception occurred while processing message.
System.InvalidOperationException: IDX10803: Unable to obtain configuration from: 'http://localhost/.well-known/openid-configuration'. ---> System.IO.IOException: IDX10804: Unable to retrieve document from: 'http://localhost/.well-known/openid-configuration'. ---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).

根据我的研究,当权威设置为与托管服务器不同的主机时,有时会触发此问题。例如,Kestrel 默认以http://localhost:5000 运行,这是我最初设置的权限,但在将其设置为 TestServer 正在模拟的内容 (http://localhost) 时,它仍然给出相同的错误。这是我的身份验证配置:

    app.UseJwtBearerAuthentication(new JwtBearerOptions
    {
        AutomaticAuthenticate = true,
        AutomaticChallenge = true,
        RequireHttpsMetadata = false,
        TokenValidationParameters = new TokenValidationParameters
        {
            ValidateIssuerSigningKey = true,
            IssuerSigningKey = signingKey,
            ValidateAudience = true
        },
        Audience = "Anything",
        Authority = "http://localhost"
    });

奇怪的是,尝试直接从集成测试中访问 URL 效果很好:

那么如何配置 ASP.Net TestServer 和 OpenId Connect 基础架构以协同工作?

=== 编辑 ====

稍微思考一下,我想到问题是 JWT 授权内部试图向 http://localhost 端口 80 发出请求,但它没有尝试使用 TestServer 和因此正在寻找一个真正的服务器。由于没有,它永远不会进行身份验证。看起来下一步是看看是否有某种方法可以关闭权威检查或以某种方式扩展基础架构以允许它使用 TestServer 作为主机。

【问题讨论】:

  • 您的编辑是正确的。您是否尝试过在测试中提供未设置权限的配置?从技术上讲,您的所有测试都应该需要一个经过身份验证的用户 - 您可以即兴创作/模拟 - 根本不必使用 JWT 身份验证。
  • 我相信我尝试不设置权限,但我似乎记得它抛出了一个错误。我最终找到了解决方案,并将发布答案。

标签: asp.net-core openid-connect identityserver4 asp.net-core-testhost


【解决方案1】:

默认情况下,JWT 基础架构确实会尝试发出 HTTP 请求。我可以通过将 JwtBearerOptions.ConfigurationManager 属性设置为 OpenIdConnectionConfigurationRetriever() 的新实例来使其工作,该实例提供了 DI 提供的 IDocumentResolver:

        ConfigurationManager = new ConfigurationManager<OpenIdConnectConfiguration>(
            authority + "/.well-known/openid-configuration",
            new OpenIdConnectConfigurationRetriever(),
            _documentRetriever),

在生产代码中,我只是将默认值注册到我的容器(Autofac)中:

builder.RegisterType<HttpDocumentRetriever>().As<IDocumentRetriever>();

我已经为我的集成测试使用了一个派生的 Setup 类,它遵循模板方法模式来配置容器,因此我能够用一个从 TestServer 实例返回结果的实例来覆盖 IDocumentRetriever 实例。

我确实遇到了一个额外的障碍,即在发出请求(从 JWT 调用我的 IDocumentRetriever 发起的请求)时,TestServer 的客户端似乎挂起,而另一个请求已经未完成(发起请求的请求以),所以我必须事先提出请求并从我的 IDocumentRetriever 中提供缓存的结果:

public class TestServerDocumentRetriever : IDocumentRetriever
{
    readonly IOpenIdConfigurationAccessor _openIdConfigurationAccessor;

    public TestServerDocumentRetriever(IOpenIdConfigurationAccessor openIdConfigurationAccessor)
    {
        _openIdConfigurationAccessor = openIdConfigurationAccessor;
    }

    public Task<string> GetDocumentAsync(string address, CancellationToken cancel)
    {
        return Task.FromResult(_openIdConfigurationAccessor.GetOpenIdConfiguration());
    }
}

【讨论】:

  • 嗨@Derek 我希望你不介意我为其他使用 IdentityServer4 的人劫持这个线程并尝试进行集成测试。我在这个线程stackoverflow.com/questions/39390339/… 中找到了解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-15
  • 2020-03-07
  • 2020-02-08
  • 2018-04-18
  • 1970-01-01
  • 2019-09-13
相关资源
最近更新 更多