【发布时间】:2017-09-22 02:47:19
【问题描述】:
我有 ASP.NET MVC Web 应用程序。
当我将它上传到 Azure Web 应用程序时,网站有 http://
我需要它始终是https://。
我该怎么做?
我知道这可能是一个过于宽泛的问题,但你能给我一些建议吗?
我在我的项目中设置了这样的属性
【问题讨论】:
标签: c# asp.net asp.net-mvc azure azure-web-app-service
我有 ASP.NET MVC Web 应用程序。
当我将它上传到 Azure Web 应用程序时,网站有 http://
我需要它始终是https://。
我该怎么做?
我知道这可能是一个过于宽泛的问题,但你能给我一些建议吗?
我在我的项目中设置了这样的属性
【问题讨论】:
标签: c# asp.net asp.net-mvc azure azure-web-app-service
我需要它始终是 https://。
您可以尝试创建 URL 重写规则来强制执行 HTTPS。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!-- BEGIN rule TAG FOR HTTPS REDIRECT -->
<rule name="Force HTTPS" enabled="true">
<match url="(.*)" ignoreCase="false" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
</rule>
<!-- END rule TAG FOR HTTPS REDIRECT -->
</rules>
</rewrite>
</system.webServer>
</configuration>
您也可以参考Enforce HTTPS on your app。
【讨论】:
如果您更喜欢通过代码而不是 web.config 的解决方案,您可以在 Global.asax.cs 文件中编写以下内容。
protected void Application_BeginRequest() {
// Ensure any request is returned over SSL/TLS in production
if (!Request.IsLocal && !Context.Request.IsSecureConnection) {
var redirect = Context.Request.Url.ToString().ToLower(CultureInfo.CurrentCulture).Replace("http:", "https:");
Response.Redirect(redirect);
}
}
【讨论】: