asp.net - 混合 http/https 环境中的 AspNet.Security.OpenIdConnect.Server (ASP.NET vNext) 权限配置

标签 asp.net jwt openid-connect aspnet-contrib

我正在使用 Visual Studio 2015 Enterprise 和 ASP.NET vNext Beta8 构建一个同时发出和使用 JWT token 的端点,如详细描述 here 。正如该文章中所解释的,端点使用 AspNet.Security.OpenIdConnect.Server(AKA OIDC)来完成繁重的工作。

在我们的内部开发环境中建立此原型(prototype)时,我们在将其与负载均衡器一起使用时遇到了问题。特别是,我们认为这与 app.UseJwtBearerAuthentication 上的“Authority”设置以及我们独特的 http/https 组合有关。在我们的负载平衡环境中,任何使用 token 调用 REST 方法的尝试都会产生此异常:

WebException: The remote name could not be resolved: 'devapi.contoso.com.well-known' HttpRequestException: An error occurred while sending the request.
IOException: IDX10804: Unable to retrieve document from: 'https://devapi.contoso.com.well-known/openid-configuration'.


考虑以下重现步骤(这是用于原型(prototype)设计,不应被视为值得生产):

  • 我们使用 OIDC 创建了 beta8 原型(prototype),如 here 所述。 .

  • 我们将该项目部署到在 Server 2012 R2 上运行的 2 台配置相同的 IIS 8.5 服务器。 IIS 服务器托管一个名为“API”的 beta8 站点,该站点在所有可用 IP 地址上绑定(bind)到主机名“devapi.contoso.com”(针对本文目的已进行清理)的端口 80 和 443。

  • 两台 IIS 服务器都有一个指向自身的主机条目:

    127.0.0.1 devapi.contoso.com

  • 我们的网络管理员已将 * 证书 (*.contoso.com) 与我们的 Kemp 负载均衡器绑定(bind),并为 https://devapi.contoso.com 配置了 DNS 条目。解决负载均衡器。

  • 现在这一点很重要,负载均衡器还配置为使用 http 将 https 流量代理到 IIS 服务器(不是,重复,不在 https 上)。有人向我解释说这是我们公司的标准操作程序,因为他们只需将证书安装在一处。 我们不确定为什么我们的网络管理员在 IIS 中绑定(bind) 443,因为理论上它永远不会在此端口上接收任何流量。

  • 我们通过 https 发送安全帖子至 https://devapi.contoso.com/authorize/v1获取 token ,效果很好(如何制作这篇文章的详细信息是 here ):

    {
    “子”:“待办事项”,
    "iss": "https://devapi.contoso.com/ ",
    "aud": "https://devapi.contoso.com/ ",
    “经验”:1446158373,
    “nbf”:1446154773
    }

  • 然后,我们在另一个安全获取中使用此 token ,通过 https 访问 https://devapi.contoso.com/values/v1/5 .

  • OpenIdConnect.OpenIdConnectConfigurationRetriever 抛出异常:

WebException: The remote name could not be resolved: 'devapi.contoso.com.well-known' HttpRequestException: An error occurred while sending the request.
IOException: IDX10804: Unable to retrieve document from: 'https://devapi.contoso.com.well-known/openid-configuration'.


我们认为发生这种情况是因为 OIDC 正在尝试咨询“options.Authority”中指定的主机,我们在启动时将其设置为 https://devapi.contoso.com/ 。此外,我们推测,由于我们的环境已配置为将负载均衡器和 IIS 之间的 https 流量转换为非 https 流量,所以当框架尝试解析 https://devapi.contoso.com/ 时,会出现问题。 。我们尝试了许多配置更改,包括甚至将权限指向非安全 http://devapi.contoso.com没有效果。

如果您能帮助我们理解这个问题,我们将不胜感激。

最佳答案

@Pinpoint 是对的。此异常是由允许 IdentityModel 发起非 HTTPS 调用的 OIDC 配置代码路径引起的。特别是,我们使用的代码示例对授权 URI 中缺少尾部斜杠很敏感。下面是一个代码片段,它使用 Uri 类以可靠的方式组合路径,无论 Authority URI 是否有尾部斜杠:

public void Configure(IApplicationBuilder app, IOptions<AppSettings> appSettings)
{
    .
    .
    .
    // Add a new middleware validating access tokens issued by the OIDC server.
    app.UseJwtBearerAuthentication
    (
        options => 
        {
            options.AuthenticationScheme    = JwtBearerDefaults.AuthenticationScheme                ;
            options.AutomaticAuthentication = false                                                 ;
            options.Authority               = new Uri(appSettings.Value.AuthAuthority).ToString()   ;
            options.Audience                = new Uri(appSettings.Value.AuthAuthority).ToString()   ;

            // Allow IdentityModel to use HTTP
            options.ConfigurationManager = 
                new ConfigurationManager<OpenIdConnectConfiguration>
                (
                    metadataAddress : new Uri(new Uri(options.Authority), ".well-known/openid-configuration").ToString(),
                    configRetriever : new OpenIdConnectConfigurationRetriever()                                         ,
                    docRetriever    : new HttpDocumentRetriever { RequireHttps = false }
                );
        }
    );
    .
    .
    .
}

在此示例中,我们通过“appSettings.Value.AuthAuthority”从 config.json 中提取权限 URI,然后使用 Uri 对其进行清理/组合。类。

关于asp.net - 混合 http/https 环境中的 AspNet.Security.OpenIdConnect.Server (ASP.NET vNext) 权限配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33426127/

相关文章:

api - 在 JWT 中设置角色是最佳实践吗?

angularjs - 如何在 angularjs 中使用拦截器刷新 JWT token ?

c# - 在 Razor 中使用静态变量

javascript - 为什么 "alert"函数没有触发?

asp.net - IdentityServer4.Services.InMemory 不工作

python - WebSocket JWT Token 连接授权

azure - 访问 IIS 托管的 Web 应用程序,该应用程序通过 PowerShell 使用 AzureAD 凭据来使用 API

asp.net-core - Identity Server 4 - 如何定义支持的授权类型等

c# - Blazor Wasm 授权状态记录多次

c# - 我可以使用 HTTP 模块更改传入 HTTP 请求的内容吗?