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中指定它的方法
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.
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)
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上查看代码.
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)
你需要做的是:
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)
在 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 的详细信息,请参见此处。