【发布时间】:2018-06-16 15:21:54
【问题描述】:
我有一个使用 ASP.NET Core MVC5 用 C# 编写的网站,我将其部署到 Google Cloud App Engine Flex 上。我想完全禁用 HTTP,只允许 HTTPS。我不想从 HTTP 重定向到 HTTPS。我该怎么做?
我尝试搜索,但找不到任何在 .NET 和 App Engine Flex 的 app.yaml 中指定它的方法。所以我尝试在网络服务器代码本身内完成它。起初作为测试,我尝试使用 RequireHttps 属性,还尝试了 RequireHttpsOrClose (https://www.recaffeinate.co/post/enforce-https-aspnetcore-api/) 的实现,这更接近我真正想要的。
当我构建项目并通过 IIS(即不是谷歌应用引擎)进行部署时,一切正常。但是,当我部署到 Google App Engine 时,它们无法正常工作。当我尝试连接它们时,它似乎总是以 HTTP 的形式出现,即使我用来连接的 URL 设置为 HTTPS。当我使用 RequireHttps 属性时,我收到“重定向过多”错误,这告诉我它正在接受我的 HTTPS 请求(例如“https://www.test.com”),但在控制器中,它看起来好像我发送了一个普通的 HTTP 请求(例如,它看到的是“http://www.test.com”)。因此,它再次将此视为假定的纯 HTTP 请求(尽管实际上并非如此),并尝试再次重定向到 HTTPS 请求,该请求再次作为纯 HTTP 请求处理。而且这似乎会无限继续,导致重定向循环。
为了进一步测试,我检查了Request.IsHttps 的值,我总是得到false,即使我肯定是用HTTPS 连接的。因此,出于某种原因,即使我使用 HTTPS 请求,该站点也只接收 HTTP 请求。
同样,当我在 App Engine 之外进行测试时(通过 IIS 在我们的非 Google 网络服务器上进行部署),一切正常。只有当我在 App Engine 上进行部署时,才会出现这种奇怪的行为。
我可以通过使用“Google Cloud Platform 上的 ASP.NET Core”模板设置准系统项目来重现这一点。唯一相关的代码是我的控制器,它是准系统,只返回模板提供的虚拟值:
[RequireHttps]
[Route("/")]
public class ValuesController : Controller
{
private readonly ILogger _logger;
public ValuesController(ILogger<ValuesController> logger)
{
_logger = logger;
}
// GET api/values
[HttpGet]
public IEnumerable<string> Get()
{
// When I remove the RequestHttps attribute in order to stop the redirect
// loop, I always get values "not https" and "HTTP/1.1" even though
// my browser is connecting to https://xxxxx.appspot.com
if (Request.IsHttps) return new string[] { "https", Request.Protocol };
return new string[] { "not https", Request.Protocol };
}
}
我使用app.yaml 发布此内容:
service: testservice
runtime: aspnetcore
env: flex
web.config 未从项目模板中修改:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
</system.webServer>
</configuration>
所以我的问题是:
- 为什么当我确定要输入 HTTPS URL 时才收到 HTTP 请求?我是否在 Google Cloud 或 App Engine 中做了什么或设置了什么导致这种情况发生?
- 否则,是否有其他方法可以禁用 HTTP 并仅允许使用带有 .NET 的 App Engine Flex 的 HTTPS?我能找到的所有禁用它的示例都是针对在
app.yaml中使用handler的Python,但这似乎不适用于.NET。
【问题讨论】:
标签: c# asp.net asp.net-mvc google-app-engine