2017-04-25 117 views
2

我有ASP.NET MVC的Web應用程序。https默認ASP.NET MVC

當我把它上傳到Azure上的Web應用程序,網站上有http://

我需要它是https://始終。

我該怎麼做?

我知道這可能是太廣泛的問題,但你能給我一些建議嗎?

enter image description here

我設置這樣的屬性在我的項目

回答

4

我需要它是https://開頭總是這樣。

您可以嘗試創建URL重寫規則以強制執行HTTPS。

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
    <system.webServer> 
    <rewrite> 
     <rules> 
     <!-- BEGIN rule TAG FOR HTTPS REDIRECT --> 
     <rule name="Force HTTPS" enabled="true"> 
      <match url="(.*)" ignoreCase="false" /> 
      <conditions> 
      <add input="{HTTPS}" pattern="off" /> 
      </conditions> 
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" /> 
     </rule> 
     <!-- END rule TAG FOR HTTPS REDIRECT --> 
     </rules> 
    </rewrite> 
    </system.webServer> 
</configuration> 

你也可以參考Enforce HTTPS on your app

+0

感謝這有助於 –

2

如果您更喜歡通過代碼解決方案而不是web.config,您可以在Global.asax.cs文件中編寫以下代碼。

protected void Application_BeginRequest() { 
      // Ensure any request is returned over SSL/TLS in production 
      if (!Request.IsLocal && !Context.Request.IsSecureConnection) { 
       var redirect = Context.Request.Url.ToString().ToLower(CultureInfo.CurrentCulture).Replace("http:", "https:"); 
       Response.Redirect(redirect); 
      } 
} 
+0

謝謝它也有幫助 –