【问题标题】:https on default ASP.NET MVC默认 ASP.NET MVC 上的 https
【发布时间】:2017-09-22 02:47:19
【问题描述】:

我有 ASP.NET MVC Web 应用程序。

当我将它上传到 Azure Web 应用程序时,网站有 http://

我需要它始终是https://

我该怎么做?

我知道这可能是一个过于宽泛的问题,但你能给我一些建议吗?

我在我的项目中设置了这样的属性

【问题讨论】:

    标签: c# asp.net asp.net-mvc azure azure-web-app-service


    【解决方案1】:

    我需要它始终是 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

    【讨论】:

    • 并且不要忘记检查服务器是否安装了 URL 重写模块,如 post 所述
    【解决方案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);
               }
    }
    

    【讨论】:

    • 谢谢它也有帮助
    猜你喜欢
    • 1970-01-01
    • 2010-10-19
    • 2010-11-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-01
    • 2021-09-03
    相关资源
    最近更新 更多