试图保护网站还要保留Home(索引)页面公共MCV5 asp.net身份

问题描述 投票:1回答:1

我有一个有几页的网站,我实现了登录和注册。

然后我使用以下代码为没有使用HTTPS访问网站的任何人实施了SSL并添加了重定向:

首先,我将项目更改为仅在项目设置enter image description here中允许SSL HTTPS

全球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:总是有效(我认为这是一个问题)

是/我:

  • 当使用https工作时(我认为这是一个问题,因为我没有登录(当匿名被删除)。
  • 如果我使用常规URL http://localhost:50499,页面将不会加载,但如果我添加allowannonymos它应该工作,它不是。 (如果我附加调试器它从未命中asax代码但仍被拒绝)
security ssl model-view-controller https authorization
1个回答
0
投票

我不确定我理解这个问题。如果您使用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;
© www.soinside.com 2019 - 2024. All rights reserved.