2016-09-22 111 views
0

如果我键入example.com,它将按照我的意愿重定向到https://example.com。但是打字http://example.com/blog当它仍然重定向到https://example.com将HTTP重定向到所有页面的HTTPS

<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添加到任何用户写的吗?

+0

你在这里试过了吗? http://stackoverflow.com/a/47095/993547 –

+0

是的。我已经添加了出站规则,现在仍然是,它重定向到根 – crystyxn

+0

请尝试此http://stackoverflow.com/questions/32523540/http-to-https-rewrite-too-many-redirect-loops-iis-7有关详细信息,请按照此文章http://www.hanselman.com/blog/HowToEnableHTTPStrictTransportSecurityHSTSInIIS7.aspx –

回答

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> 
+0

的BlogController?还是家? – crystyxn

+0

@crystyxn在您需要使用https的所有控制器上使用[RequireHttps]属性。 – qin

+0

即时通讯使用ASP.NET MVC 5 – crystyxn