【问题标题】:HTTP to HTTPS redirection in asp.net not workingasp.net 中的 HTTP 到 HTTPS 重定向不起作用
【发布时间】:2012-06-19 14:49:48
【问题描述】:

我们有一个简单的要求,即对 asp.net 4.0 Web 应用程序中的某些特定页面使用 https。为了检查我们的实现,我们将一个简单的 asp.net 4.0 应用程序部署到 IIS 7。该应用程序已被编码为基于 web.config 标志通过 https 将 default.aspx 页面重定向到 securepage.aspx。

protected void Page_Load(object sender, EventArgs e)
    {
        Uri requestUri = Page.Request.Url;
        UriBuilder builder = new UriBuilder("https", requestUri.Host, requestUri.Port, "SecurePage.aspx");
        string secureUrl = builder.Uri.ToString();

        if (bool.Parse(ConfigurationManager.AppSettings["UseSecure"]))
        {
            Response.Redirect(secureUrl, true);
        }
        else
        {
            Response.Write(secureUrl);
        }
    }

但是我们在 IIS 7 上部署这个应用程序并加载 default.aspx 页面后,它显示“Internet Explorer 无法显示该网页”。但是如果我们关闭配置标志,它会正确显示页面。该应用程序在端口 82 上具有 http 绑定,在端口 444 上具有 https。

谁能指出我们哪里出错了。

【问题讨论】:

  • 两个答案中的女巫是一个有效的,一个是http,还是一个是https?或两者 ?因为答案相差这么小。
  • 两者都将根据需要工作。重点是https端口与http请求端口相同

标签: asp.net https


【解决方案1】:

当您在前面输入 https 时,浏览器会在端口 443 上而不是在 444 上,因此要在您的自定义端口上移动,您需要将其输入为。

UriBuilder builder = new UriBuilder("http", requestUri.Host, 444, "SecurePage.aspx");

【讨论】:

    【解决方案2】:

    您正在通过 requestUri.Port 使用当前请求的端口,它返回 80。您应该硬编码 444:

    UriBuilder builder = new UriBuilder("https", requestUri.Host, 444, "SecurePage.aspx");
    

    如果端口发生变化,则使用可配置变量。

    【讨论】:

      猜你喜欢
      • 2014-03-10
      • 2018-06-11
      • 2015-07-28
      • 2022-01-14
      • 2021-04-15
      • 2018-01-31
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      相关资源
      最近更新 更多