asp.net中强制https用于整个网站的最佳方式?

cod*_*ift 185 c# vb.net asp.net https webforms

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

有没有更好的方法 - 理想情况下web.config中的一些设置?

Joh*_*ker 236

请使用HSTS

来自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>
Run Code Online (Sandbox Code Playgroud)

原始答案(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);
   }
}
Run Code Online (Sandbox Code Playgroud)

那将放在global.asax.cs(或global.asax.vb)中

我不知道在web.config中指定它的方法

  • 请注意,这并不提供任何有用的安全性.实际上,它只会保护已经安全的用户的连接,并且无法保护那些受到攻击的用户(这是因为MITM可以简单地省略重定向并将所有内容转发到您的"安全"站点).恕我直言,重定向用户代理只是感觉良好的伏都教安全性,并提供有时危险的安全错觉.唯一的方法是指示用户代理仅请求安全资源,如果不这样做则不重定向.这就是HSTS的作用 - 见下面的答案. (9认同)
  • 这有效,但这对我来说很危险:当我尝试在运行此代码的VS 2010中本地运行时,我的起始页面从未加载; 相反,我刚刚收到"此网页不可用"的消息.为了解决这个问题,我添加了第二个条件来测试url是否包含字符串"localhost":如果没有,则强制使用https. (6认同)
  • 这给了我一个重定向循环.在我添加代码之前,它运行正常.有什么建议? (2认同)
  • 这个答案应被视为"有害",不应使用.根据以上@tne的评论. (2认同)
  • @RosdiKasim自2015年12月4日编辑以来,这个答案是否仍然被视为有害? (2认同)

Tro*_*unt 117

您可以做的另一件事是通过将"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;
  }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 你需要将开关包装在`if(!Request.IsLocal)`中,这样它就不会破坏调试. (16认同)
  • 从来没有听说过HSTS标题,但看起来很酷.是否有任何理由使用如此小的最大年龄值(5分钟)?您链接到的维基百科文章建议将其设置为较大的值(6-12个月). (5认同)
  • +1.查看Troy博客上这篇非常广泛的文章,其中详细介绍了仅使用重定向可以降低安全性的原因.提示:除了其他方面,它还会让您容易受到SSL Strip工具的攻击.http://www.troyhunt.com/2011/11/owasp-top-10-for-net-developers-part-9.html (5认同)
  • 另外值得查看[NWebsec](https://nwebsec.codeplex.com/),这使得这个(以及更多)非常容易. (3认同)

Mar*_*ark 87

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>
Run Code Online (Sandbox Code Playgroud)

  • 对于任何想知道的人来说,这都在`<system.webServer>`里面. (41认同)
  • 此外,对于IIS 7.0,您需要安装Url Rewrite Module 2.0 (10认同)

Muh*_*eed 21

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

困难的方式

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

GlobalFilters.Filters.Add(new RequireHttpsAttribute());
Run Code Online (Sandbox Code Playgroud)

2 - 强制防伪标记使用SSL/TLS:

AntiForgeryConfig.RequireSsl = true;
Run Code Online (Sandbox Code Playgroud)

3 - 通过更改Web.config文件,默认情况下要求Cookie要求HTTPS:

<system.web>
    <httpCookies httpOnlyCookies="true" requireSSL="true" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

4 - 使用NWebSec.Owin NuGet包并添加以下代码行以在站点上启用严格传输安全性.不要忘记在下面添加Preload指令并将您的站点提交到HSTS Preload站点.更多信息在这里这里.请注意,如果您不使用OWIN,则可以在NWebSec站点上读取Web.config方法.

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

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));
Run Code Online (Sandbox Code Playgroud)

6 - 在所使用的任何URL中包含https方案.内容安全策略(CSP) HTTP标头和子资源完整性(SRI)在某些浏览器中模仿该方案时效果不佳.最好明确HTTPS.例如

<script src="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.4/bootstrap.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

简单的方法

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

  • 另外如果使用`<authentication mode ="Forms">`,你必须有`<forms requireSSL ="true">` (3认同)

Fly*_*wat 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

 <httpModules>
      <add name="HttpsOnlyModule" type="HttpsOnly.HttpsOnlyModule, HttpsOnly" />
 </httpModules>
Run Code Online (Sandbox Code Playgroud)

  • 我有点困惑.我希望`app.BeginRequest + = new OnBeginRequest;``init`方法和`OnBeginRequest`中的`将包含当前`Init`方法包含的内容.你确定这个模块按预期工作吗? (2认同)

Cha*_*mar 5

你需要做的是:

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

<add key="HttpsServer" value="stage"/>
             or
<add key="HttpsServer" value="prod"/>
Run Code Online (Sandbox Code Playgroud)

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mik*_*ike 5

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

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

我在这里概述了两个示例方法,以避免链接失效。

方法 1 - 直接编辑 ApplicationHost.config 文件 在<site>标签之间添加以下行:

<hsts enabled="true" max-age="31536000" includeSubDomains="true" redirectHttpToHttps="true" />
Run Code Online (Sandbox Code Playgroud)

方法 2 - 命令行:从提升的命令提示符执行以下命令(即在 CMD 上右键并以管理员身份运行)。请记住将 Contoso 替换为 IIS 管理器中显示的站点名称。

c:
cd C:\WINDOWS\system32\inetsrv\
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.enabled:True" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.max-age:31536000" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.includeSubDomains:True" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Contoso'].hsts.redirectHttpToHttps:True" /commit:apphost
Run Code Online (Sandbox Code Playgroud)

如果您位于访问权限有限的托管环境中,Microsoft 在该文章中提供的其他方法可能是更好的选择。

请记住,IIS10 版本 1709 现已在 Windows 10 上提供,但对于 Windows Server 2016,它位于不同的发布轨道上,并且不会作为补丁或服务包发布。有关 1709 的详细信息,请参见此处