在 asp.net 中为整个站点强制使用 https 的最佳方式?
题
大约 6 个月前,我推出了一个网站,其中每个请求都需要通过 https 进行。当时我能找到的确保每个页面请求都通过 https 的唯一方法是在页面加载事件中检查它。如果请求不是通过http我会response.redirect("https://example.com")
有没有更好的方法——最好是在 web.config 中进行一些设置?
解决方案
请使用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>
原答案 (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 中指定它的方法
其他提示
你可以做的另一件事是使用 HSTS 通过将“Strict-Transport-Security”标头返回给浏览器。浏览器必须支持这一点(目前主要是 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。
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>
对于那些使用 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 包并添加以下代码行以在整个站点启用严格传输安全。不要忘记添加下面的 Preload 指令并将您的网站提交到 HSTS 预加载站点. 。更多信息 这里 和 这里. 。请注意,如果您不使用 OWIN,则可以在以下位置阅读 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 样板 Visual Studio 项目模板可生成包含所有这些内容以及更多内置内容的项目。您还可以查看代码 GitHub.
如果您出于某种原因无法在 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>
你需要做的是:
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);
}
}
}
}
如果您的站点中无法配置 SSL 支持(即应该能够打开/关闭 https) - 您可以在您希望保护的任何控制器/控制器操作上使用 [RequireHttps] 属性。
它还取决于您的平衡器的品牌,对于网络复用器,您需要查找 http 标头 X-WebMux-SSL-termination: true
以确定传入流量是 ssl。详细信息在这里: http://www.cainetworks.com/support/redirect2ssl.html
对于上面的@Joe,“这给了我一个重定向循环。在我添加代码之前它工作得很好。有什么建议么?– Joe 2011 年 11 月 8 日 4:13"
这也发生在我身上,我认为发生的情况是负载均衡器在 Web 服务器前面终止了 SSL 请求。因此,我的网站始终认为请求是“http”,即使原始浏览器请求它是“https”。
我承认这有点hacky,但对我有用的是实现一个“JustRedirected”属性,我可以利用它来确定该人已经被重定向过一次。因此,我测试了保证重定向的特定条件,如果满足这些条件,我会在重定向之前设置此属性(存储在会话中的值)。即使第二次满足重定向的 http/https 条件,我也会绕过重定向逻辑并将“JustRedirected”会话值重置为 false。您需要自己的条件测试逻辑,但这是该属性的简单实现:
public bool JustRedirected
{
get
{
if (Session[RosadaConst.JUSTREDIRECTED] == null)
return false;
return (bool)Session[RosadaConst.JUSTREDIRECTED];
}
set
{
Session[RosadaConst.JUSTREDIRECTED] = value;
}
}
我要投入两分钱。 如果 您可以访问 IIS 服务器端,然后您可以使用协议绑定强制使用 HTTPS。例如,您有一个名为 废话. 。在 IIS 中,您需要设置两个站点: 废话, , 和 巴拉(重定向). 。为了 废话 只需配置 HTTPS
绑定(和 FTP
如果需要,请确保也通过安全连接强制它)。为了 巴拉(重定向) 只需配置 HTTP
捆绑。最后,在 HTTP 重定向 部分 巴拉(重定向) 确保将 301 重定向设置为 https://blah.com
, ,启用精确目的地。确保 IIS 中的每个站点都指向它的 自己的 根文件夹,否则 网页配置 会搞砸的。还要确保有 HSTS
在您的 HTTPSed 站点上进行配置,以便浏览器的后续请求始终强制为 HTTPS,并且不会发生重定向。
这是基于@Troy Hunt 的更完整的答案。将此功能添加到您的 WebApplication
班级在 Global.asax.cs
:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
// Allow https pages in debugging
if (Request.IsLocal)
{
if (Request.Url.Scheme == "http")
{
int localSslPort = 44362; // Your local IIS port for HTTPS
var path = "https://" + Request.Url.Host + ":" + localSslPort + Request.Url.PathAndQuery;
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", path);
}
}
else
{
switch (Request.Url.Scheme)
{
case "https":
Response.AddHeader("Strict-Transport-Security", "max-age=31536000");
break;
case "http":
var path = "https://" + Request.Url.Host + Request.Url.PathAndQuery;
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", path);
break;
}
}
}
(要在本地构建上启用 SSL,请在项目的“属性”面板中启用它)
-> 只需在公共类 HomeController 之上添加 [RequireHttps] :控制器。
-> 并添加 GlobalFilters.Filters.Add(new RequireHttpsAttribute());在 Global.asax.cs 文件中的“protected void Application_Start()”方法中。
这会强制您的整个应用程序使用 HTTPS。
我花了一些时间寻找有意义的最佳实践,并发现以下最适合我。我希望这能在某个时候拯救你。
使用 配置文件 (例如 asp.net 网站)https://blogs.msdn.microsoft.com/kaushal/2013/05/22/http-to-https-redirects-on-iis-7-x-and-higher/
或者在您自己的服务器上https://www.sslshopper.com/iis7-redirect-http-to-https.html
简短答案]简单的代码在内部
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP/S to HTTPS Redirect" enabled="true"
stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{SERVER_PORT_SECURE}" pattern="^0$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
如果您使用 ASP.NET Core,您可以尝试 nuget 包 SaidOut.AspNetCore.HttpsWithStrictTransportSecurity。
那么你只需要添加
app.UseHttpsWithHsts(HttpsMode.AllowedRedirectForGet, configureRoutes: routeAction);
这还将向使用 https 方案发出的所有请求添加 HTTP StrictTransportSecurity 标头。
示例代码和文档 https://github.com/saidout/saidout-aspnetcore-httpswithstricttransportsecurity#example-code
在 IIS10(Windows 10 和 Server 2016)中,从版本 1709 开始,有一个新的、更简单的选项用于为网站启用 HSTS。
微软描述了新方法的优点 这里, ,并提供了许多不同的示例,说明如何以编程方式或直接编辑 ApplicationHost.config 文件(类似于 web.config,但在 IIS 级别而不是单个站点级别运行)来实现更改。ApplicationHost.config 可以在 C:\Windows\System32\inetsrv\config 中找到。
我在这里概述了两个示例方法,以避免链接失效。
方法一 - 直接在 <site>
标签,添加这一行:
<hsts enabled="true" max-age="31536000" includeSubDomains="true" redirectHttpToHttps="true" />
方法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
如果您位于访问权限有限的托管环境中,Microsoft 在该文章中提供的其他方法可能是更好的选择。
请记住,IIS10 版本 1709 现已在 Windows 10 上提供,但对于 Windows Server 2016,它位于不同的发布轨道上,并且不会作为补丁或服务包发布。看 这里 有关1709的详细信息。