【问题标题】:Url rewriting with https使用 https 重写 URL
【发布时间】:2014-03-19 14:59:22
【问题描述】:

我有以下网址

http://www.mywebsite.com/home

https://www.mywebsite.com/secure/reports/

当用户输入上述网址时,我们会加载

http://www.mywebsite.com/home.aspx

https://www.mywebsite.com/secure/reports.aspx

问题是,如果用户输入像https://www.mywebsite.com/home,需要重定向到http://www.mywebsite.com/home。只需删除将 https 更改为 http,因为它不安全

同样,如果用户输入http://www.mywebsite.com/secure/reports,我们需要还原为安全https://www.mywebsite.com/secure/reports

【问题讨论】:

    标签: c# asp.net url url-rewriting asp.net-4.0


    【解决方案1】:

    this:

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {
       if (HttpContext.Current.Request.IsSecureConnection.Equals(false) && HttpContext.Current.Request.IsLocal.Equals(false))
       {
        Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
    +   HttpContext.Current.Request.RawUrl);
       }
    }
    

    【讨论】:

    • 用户故意将https 更改为http,反之亦然。我想阻止这种情况。您的代码适用于这两种情况吗?
    【解决方案2】:

    对您的 Web.Config 进行更改

    <rule name="Redirect to HTTPS" stopProcessing="true">
    <match url="(.*)" />
    <conditions><add input="{HTTPS}" pattern="^OFF$" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
    </rule>
    

    更多参考: 检查-http://www.jppinto.com/2010/03/automatically-redirect-http-requests-to-https-on-iis7-using-url-rewrite-2-0/

    【讨论】:

      猜你喜欢
      • 2015-05-22
      • 1970-01-01
      • 2018-02-20
      • 2021-12-28
      • 2016-06-30
      • 2014-10-06
      • 2021-01-30
      • 2012-06-17
      • 1970-01-01
      相关资源
      最近更新 更多