如何在 MVC 应用程序 (IIS7.5) 中将 HTTP 重定向到 HTTPS [英] How to redirect HTTP to HTTPS in MVC application (IIS7.5)

查看:28
本文介绍了如何在 MVC 应用程序 (IIS7.5) 中将 HTTP 重定向到 HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的 HTTP 站点重定向到 HTTPS,已添加以下规则,但尝试使用 http:/时出现 403 错误/www.example.com,当我输入 https://www.example.com 在浏览器中.

I need to redirect my HTTP site to HTTPS, have added below rule but I am getting 403 Error when tried using http://www.example.com, it works fine when I type https://www.example.com in browser.

<system.webServer>
    <rewrite>
        <rules>
            <rule name="HTTP to HTTPS redirect" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                </conditions>
                <action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

推荐答案

我在 Global.asax 中使用了以下内容:

I use the following in Global.asax:

protected void Application_BeginRequest()
{
  if (FormsAuthentication.RequireSSL && !Request.IsSecureConnection)
  {
    Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"));
  }
}

这篇关于如何在 MVC 应用程序 (IIS7.5) 中将 HTTP 重定向到 HTTPS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆