c# - Azure App Service 在应用程序之前终止 https?
问题描述
我正在框架 2.2 上构建一个 asp.net 核心 Web 应用程序,并在 linux 应用程序服务计划上的一个 azure 应用程序服务上托管。
在我的应用程序中,我检查HttpRequest.Scheme。https
如果我使用 https 发出请求,则在本地运行会返回。在天蓝色上运行它返回http
。
Azure App Services 似乎正在终止 SSL 连接并代理到我的应用程序。 有没有办法配置 Azure 应用服务,以便 https 请求未修改地发送到我的应用程序?或者至少HttpRequest.Scheme匹配原始请求?
我构建了一个示例诊断页面来显示此行为:
var healthStatus = new
{
Port = context.Request.Host.Port?.ToString() ?? "unknown",
context.Request.Scheme,
context.Request.IsHttps,
Headers = context.Request.Headers.Select(x => $"{x.Key}:{x.Value}").ToArray()
};
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(JsonConvert.SerializeObject(healthStatus));
在 VS 本地调试: https://localhost:1234/ping
:
{
"Port":1234,
"Scheme": "https",
"IsHttps": true,
"Headers": <standard headers - nothing interesting>
}
部署到 Azure 应用服务https://appServiceExample.myDomain.com/ping
::
{
"Port":"unknown",
"Scheme": "http",
"IsHttps": false,
Headers: [
// there are several more headers, but only these looked interesting:
"X-Forwarded-For:195.206.xxx.xxx:6922",
"X-Forwarded-Proto:https",
"X-AppService-Proto:https"
]
}
作为一种解决方法:我可以依靠X-AppService-Proto
orX-Forwarded-Proto
标头来解决这个问题吗?但这似乎有点 hack,因为我宁愿检查原始传入请求 - 我不确定这些标头的可靠性。
解决方案
简单总结一下你的评论。
Azure 应用服务前端层终止TLS 通道(也称为 TLS 卸载)并打开一个新的纯 HTTP连接到您的代码所在的 Web Worker。路由由 ARR(应用程序请求路由)执行。
因此,从您的代码的角度来看,每个请求都是“不安全的”。
X-Forwarded-Proto=https
关于原始请求(到达前端)的提示。
如果必须进行检查,请改为进行检查X-ARR-SSL
。
有关更多详细信息,您可以参考此SO 线程。