在MVC5应用程序上强制HTTPS时的ERR_TOO_MANY_REDIRECTS

时间:2017-06-28 11:41:29

标签: c# asp.net ssl iis https

我正在使用OVH Server和SSL Gateway Free,我已经托管了我的MVC5应用程序。

当我试图仅强制使用HTTPS连接时,我的浏览器显示:

  

“ERR_TOO_MANY_REDIRECTS”。

我正在尝试各种各样的事情和解决方案,但没有一个有效。

我在我的项目属性中启用了SSL,尝试通过我的IIS上的URL重写重定向,然后是tutorial以及其他许多人,在我的<template> <nav class="navbar navbar-default" role="navigation"> <div class="container-fluid"> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">{{ msg }}</a> </div> </div> </nav> </template> <script> ... </script> <style> ... </style>上使用[RequireHttps]以及许多Global.asax中BaseController的配置。

f.e:

Application_BeginRequest()

不知道如何强制只使用HTTPS连接。

2 个答案:

答案 0 :(得分:2)

@PeteG回答是对的。 IsSecureConnection无法正常工作,您只需使用此代码:

InitializeComponent();
propertyGrid.LineColor = SystemColors.InactiveBorder;

答案 1 :(得分:-2)

因为重定向进入循环。它一次又一次地调用同一页面。像死锁一样。 使用此代码

    if (Request.Url.Scheme.Contains("http"))
{
    Response.Redirect(string.Format("https://{0}{1}", Request.Url.Authority, returnUrl), true);
}

protected void Application_BeginRequest(Object source, EventArgs e)
{
  if (!Context.Request.IsSecureConnection)
  {
      Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"));
  }
}
相关问题