【问题标题】:Redirect HTTP to HTTPS for all pages将所有页面的 HTTP 重定向到 HTTPS
【发布时间】:2017-01-31 03:01:42
【问题描述】:

如果我输入 example.com,它会根据需要重定向到 https://example.com。但是在输入http://example.com/blog 时,它仍然会重定向到https://example.com

<rewrite>
      <rules>
        <clear/>
         <rule name="Force HTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="false" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>

如何让它添加 https 到用户写的任何内容?

【问题讨论】:

标签: c# https url-rewriting web-config


【解决方案1】:

您可以为控制器使用属性 [System.Web.Mvc.RequireHttps]。

如果您在所有控制器上都需要它,您可以使用以下代码(仅限 Asp.Net MVC6/core)。

public void ConfigureServices(IServiceCollection services)
{
  services.AddMvc(options =>
    {
        options.Filters.Add(typeof(RequireHttpsInProductionAttribute));
    });
}

注意:您可以使用HSTS来避免客户端下次使用http请求。

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Strict-Transport-Security" value="max-age=31536000"/>
      </customHeaders>
    </httpProtocol>
</system.webServer>

【讨论】:

  • 博客控制器?还是家?
  • @crystyxn 在您需要使用 https 的所有控制器上使用 [RequireHttps] 属性。
  • 我正在使用 ASP.NET MVC 5
  • 只需在所有控制器上使用 [RequireHttps]。但它不能帮助你处理静态文件。
猜你喜欢
  • 1970-01-01
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-14
  • 2013-10-18
  • 2013-08-01
  • 1970-01-01
相关资源
最近更新 更多