【问题标题】:HTTPS Urls When SSL Terminated At Load BalancerSSL 在负载均衡器处终止时的 HTTPS 网址
【发布时间】:2018-08-28 17:30:08
【问题描述】:

当我返回 CreatedAtActionResult 或从我的 API 控制器使用 UriHelper.GetDisplayUrl(Request) 获取当前请求 url 时,我需要我的 ASP.NET Core 应用程序返回 https url。我不想让我的服务处理 https 流量,因此不希望使用 https 重定向。理想情况下,任何生成 url / uri 的进程都应该能够将架构覆盖为https://

背景

我在 AWS Fargate 上的 Docker 中运行了一项服务,该服务位于负载均衡器后面,该负载均衡器终止 SSL 流量并将请求作为 HTTP 转发到我的 Asp.Net Core API。该 API 具有返回 CreatedAtActionResult 的操作,该操作形成 Location 标头响应中提供的 url。不幸的是,这些网址是http:// 而不是https://,因为我的服务不知道它应该返回https://

【问题讨论】:

    标签: asp.net-core https


    【解决方案1】:

    我能够通过编写一个自定义中间件来解决这个问题,该中间件使用配置值来识别我正在运行的环境,并有选择地更改传入的请求。希望这对遇到此问题的下一个人有所帮助!

    app.Use(async (context, next) =>
    {
        // Rewrite the inbound Request when running remotely (where this key isn't set)
        if (Configuration["RunningEnvironment"] == null)
        {
            context.Request.IsHttps = true;
            context.Request.Scheme = "https";
        }
    
        await next.Invoke();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-04
      • 2020-06-28
      • 2015-01-13
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2018-02-21
      相关资源
      最近更新 更多