如何在MVC应用程序中将HTTP重定向到HTTPS(IIS7.5)

LLM*_*LLM 50 asp.net-mvc https redirect http

我需要将我的HTTP站点重定向到HTTPS,添加了下面的规则,但是当我尝试使用http://www.example.com时我得到403错误,当我在浏览器中键入https://www.example.com时,它工作正常.

<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>
Run Code Online (Sandbox Code Playgroud)

Chr*_*ken 110

你可以在代码中做到:

的Global.asax.cs

protected void Application_BeginRequest(){
    if (!Context.Request.IsSecureConnection)
        Response.Redirect(Context.Request.Url.ToString().Replace("http:", "https:"));
}
Run Code Online (Sandbox Code Playgroud)

或者您可以将相同的代码添加到操作过滤器:

public class SSLFilter : ActionFilterAttribute {

    public override void OnActionExecuting(ActionExecutingContext filterContext){
        if (!filterContext.HttpContext.Request.IsSecureConnection){
            var url = filterContext.HttpContext.Request.Url.ToString().Replace("http:", "https:");
            filterContext.Result = new RedirectResult(url);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 代码不是很正确:以下url`http://example.com?someParam = http:`将转换为`https://example.com?someParam = https:`修改参数字符串,考虑使用`string url ="https://"+ filterContext.HttpContext.Request.Url.Host + filterContext.HttpContext.Request.RawUrl;`(取自RequireHttpsAttribute也完全相同) (14认同)
  • 如果 url 的 params 中包含一个 url 或其他一些 `http:`,它也会被转换。所以我认为你应该在第 4 个位置添加 s:`Response.Redirect(Context.Request.Url.ToString().Insert(4, "s"));`。看我的回答 (3认同)

Mat*_*ier 44

Global.asax.cs:

简单的重定向

protected void Application_BeginRequest()
{
    if (!Context.Request.IsSecureConnection
        && !Context.Request.IsLocal // to avoid switching to https when local testing
        )
    {
        // Only insert an "s" to the "http:", and avoid replacing wrongly http: in the url parameters
        Response.Redirect(Context.Request.Url.ToString().Insert(4, "s"));
    }
}
Run Code Online (Sandbox Code Playgroud)

301重定向:SEO最佳实践(搜索引擎优化)

301 Moved Permanently重定向状态响应代码被认为是最好的做法升级从HTTP用户HTTPS(见谷歌的建议).

因此,如果Google或Bing机器人也将被重定向,请考虑以下因素:

protected void Application_BeginRequest()
{
    if (!Context.Request.IsSecureConnection
        && !Context.Request.IsLocal // to avoid switching to https when local testing
        )
    {
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", Context.Request.Url.ToString().Insert(4, "s"));
        Response.End();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 应该选择回答IMO,而不是我希望OP在5年后回来. (4认同)
  • 我同意上面的帖子 - 这个应该选择答案 (2认同)

小智 9

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

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

  • 它是不完整的,因为url参数中的任何“ http”也将被替换。[查看我的答案](/sf/answers/2888234611/) (2认同)

Man*_*jar 7

我在 Web.config 文件中有以下 ASP.NET MVC 重写规则:

您可以使用 web.config 文件尝试此代码。如果您的 URL 是http://www.example.com那么它将被重定向到此 URL https://www.example.com

<system.webServer>
    <rewrite>
        <rules>
             <rule name="http to https" stopProcessing="true">
              <match url="(.*)" />
              <conditions>
               <add input="{HTTPS}" pattern="^OFF$" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)