我有一个有几页的网站,我实现了登录和注册。
然后我使用以下代码为没有使用HTTPS访问网站的任何人实施了SSL并添加了重定向:
全球ASAX
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;
}
}
web.config中
<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>
当我第一次运行项目时,IIS安装了一个证书,它警告我,我也可以访问这个站点,所以当我运行项目chrome(默认浏览器)时,无论url是什么样子,总是可以访问该站点。 (首要问题)
我试图从IE或Firefox和IE浏览网站用于测试目的,如果我使用https,我永远不会点击索引页面,当然,它会点击页面但我没有登录,所以我认为这是一个问题......我补充道
[AllowAnonymous]
public ActionResult Index()
{
if (Session["userid"] != null)
{
ViewBag.UserName = Session["username"].ToString();
return View();
}
return View();
}
而且,我无法访问该网站。我很安全,请耐心等待我。我想要的行为是网站是安全的,但用户可以点击索引并注册登录等....但是当他们试图点击其他需要登录的页面时被拒绝。
总结Chrome:总是有效(我认为这是一个问题)
是/我:
我不确定我理解这个问题。如果您使用OWIN和Identity授权用户,那么为了保护您的其他控制器,您将使用以下内容
[Authorize]
public ActionResult YourSecuredController()
{
if (Session["userid"] != null)
{
ViewBag.UserName = Session["username"].ToString();
return View();
}
return View();
}
您的公共控制器在标头中没有[Authorize]属性。此外,如果您希望您的网站始终使用TLS 1.2安全,那么您可以将以下行添加到Global.asax中的Application_Start()方法。
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;