asp.net 中为整个站点强制使用 https 的最佳方式?

问题描述 投票:0回答:7

大约 6 个月前,我推出了一个网站,其中每个请求都需要通过 https 进行。当时我能找到的确保每个页面请求都通过 https 的唯一方法是在页面加载事件中检查它。如果请求不是通过http,我会response.redirect("https://example.com")

有没有更好的方法——最好是在 web.config 中进行一些设置?

c# asp.net vb.net webforms https
7个回答
275
投票

请使用HSTS(HTTP 严格传输安全)

来自 http://www.hanselman.com/blog/HowToEnableHTTPStrictTransportSecurityHSTSInIIS7.aspx

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}"
                        redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security"
                        pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000" />
                </rule>
            </outboundRules>
        </rewrite>
    </system.webServer>
</configuration>

原答案(于2015年12月4日替换为上述答案)

基本上

protected void Application_BeginRequest(Object sender, EventArgs e)
{
   if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
   {
    Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
+   HttpContext.Current.Request.RawUrl);
   }
}

这将进入 global.asax.cs (或 global.asax.vb)

我不知道如何在 web.config 中指定它


130
投票

您可以做的另一件事是通过将“Strict-Transport-Security”标头返回到浏览器来使用HSTS。浏览器必须支持这一点(目前主要是 Chrome 和 Firefox),但这意味着一旦设置,浏览器将不会通过 HTTP 向站点发出请求,而是在发出请求之前将它们转换为 HTTPS 请求。尝试将此与来自 HTTP 的重定向结合起来:

protected void Application_BeginRequest(Object sender, EventArgs e)
{
  switch (Request.Url.Scheme)
  {
    case "https":
      Response.AddHeader("Strict-Transport-Security", "max-age=300");
      break;
    case "http":
      var path = "https://" + Request.Url.Host + Request.Url.PathAndQuery;
      Response.Status = "301 Moved Permanently";
      Response.AddHeader("Location", path);
      break;
  }
}

不支持 HSTS 的浏览器只会忽略标头,但仍会被 switch 语句捕获并发送到 HTTPS。


92
投票

IIS7 模块将允许您重定向。

    <rewrite>
        <rules>
            <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
                <match url="(.*)"/>
                <conditions>
                    <add input="{HTTPS}" pattern="^OFF$"/>
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
            </rule>
        </rules>
    </rewrite>

25
投票

对于那些使用 ASP.NET MVC 的人。您可以使用以下方法通过两种方式在整个站点上强制使用 HTTPS 上的 SSL/TLS:

艰难的道路

1 - 将 RequireHttpsAttribute 添加到全局过滤器:

GlobalFilters.Filters.Add(new RequireHttpsAttribute());

2 - 强制防伪令牌使用 SSL/TLS:

AntiForgeryConfig.RequireSsl = true;

3 - 通过更改 Web.config 文件,默认需要 Cookie 来要求 HTTPS:

<system.web>
    <httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>

4 - 使用 NWebSec.Owin NuGet 包并添加以下代码行以在整个站点启用严格传输安全。不要忘记添加下面的预加载指令并将您的网站提交到HSTS预加载网站。更多信息请参见此处此处。请注意,如果您不使用 OWIN,您可以在 NWebSec 站点上阅读 Web.config 方法。

// app is your OWIN IAppBuilder app in Startup.cs
app.UseHsts(options => options.MaxAge(days: 30).Preload());

5 - 使用 NWebSec.Owin NuGet 包并添加以下代码行以在整个站点启用公钥固定 (HPKP)。更多信息这里这里

// app is your OWIN IAppBuilder app in Startup.cs
app.UseHpkp(options => options
    .Sha256Pins(
        "Base64 encoded SHA-256 hash of your first certificate e.g. cUPcTAZWKaASuYWhhneDttWpY3oBAkE3h2+soZS7sWs=",
        "Base64 encoded SHA-256 hash of your second backup certificate e.g. M8HztCzM3elUxkcjR2S5P4hhyBNf6lHkmjAHKhpGPWE=")
    .MaxAge(days: 30));

6 - 在使用的任何 URL 中包含 https 方案。当您在某些浏览器中模仿该方案时,内容安全策略(CSP)HTTP标头和子资源完整性(SRI)无法很好地发挥作用。最好明确 HTTPS。例如

<script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.4/bootstrap.min.js"></script>

简单的方法

使用 ASP.NET MVC Boilerplate Visual Studio 项目模板生成包含所有这些内容以及更多内置内容的项目。您还可以在 GitHub 上查看代码。


13
投票

如果您出于某种原因无法在 IIS 中进行设置,我会创建一个 HTTP 模块来为您执行重定向:

using System;
using System.Web;

namespace HttpsOnly
{
    /// <summary>
    /// Redirects the Request to HTTPS if it comes in on an insecure channel.
    /// </summary>
    public class HttpsOnlyModule : IHttpModule
    {
        public void Init(HttpApplication app)
        {
            // Note we cannot trust IsSecureConnection when 
            // in a webfarm, because usually only the load balancer 
            // will come in on a secure port the request will be then 
            // internally redirected to local machine on a specified port.

            // Move this to a config file, if your behind a farm, 
            // set this to the local port used internally.
            int specialPort = 443;

            if (!app.Context.Request.IsSecureConnection 
               || app.Context.Request.Url.Port != specialPort)
            {
               app.Context.Response.Redirect("https://" 
                  + app.Context.Request.ServerVariables["HTTP_HOST"] 
                  + app.Context.Request.RawUrl);    
            }
        }

        public void Dispose()
        {
            // Needed for IHttpModule
        }
    }
}

然后将其编译为 DLL,将其添加为项目的引用并将其放置在 web.config 中:

 <httpModules>
      <add name="HttpsOnlyModule" type="HttpsOnly.HttpsOnlyModule, HttpsOnly" />
 </httpModules>

5
投票

你需要做的是:

1)在 web.config 中添加一个密钥,具体取决于生产或阶段服务器,如下所示

<add key="HttpsServer" value="stage"/>
             or
<add key="HttpsServer" value="prod"/>

2)在 Global.asax 文件中添加以下方法。

void Application_BeginRequest(Object sender, EventArgs e)
{
    //if (ConfigurationManager.AppSettings["HttpsServer"].ToString() == "prod")
    if (ConfigurationManager.AppSettings["HttpsServer"].ToString() == "stage")
    {
        if (!HttpContext.Current.Request.IsSecureConnection)
        {
            if (!Request.Url.GetLeftPart(UriPartial.Authority).Contains("www"))
            {
                HttpContext.Current.Response.Redirect(
                    Request.Url.GetLeftPart(UriPartial.Authority).Replace("http://", "https://www."), true);
            }
            else
            {
                HttpContext.Current.Response.Redirect(
                    Request.Url.GetLeftPart(UriPartial.Authority).Replace("http://", "https://"), true);
            }
        }
    }
}

5
投票

在 IIS10(Windows 10 和 Server 2016)中,从版本 1709 开始,有一个新的、更简单的选项用于为网站启用 HSTS。

Microsoft 描述了新方法的优点here,并提供了许多不同的示例,说明如何以编程方式或直接编辑 ApplicationHost.config 文件(类似于 web.config,但在 IIS 级别运行,而不是个人站点级别)。 ApplicationHost.config 可以在 C:\Windows\System32\inetsr 中找到

© www.soinside.com 2019 - 2024. All rights reserved.