1

I'm using OVH Server with SSL Gateway Free where i've hosted my MVC5 App.

When i was trying to force only HTTPS connection my browser displayed:

"ERR_TOO_MANY_REDIRECTS".

I was trying various things and solutions but none of them worked.

I enabled SSL in my project properties, tried to redirect through URL Rewrite on my IIS followed by this tutorial and many others, use [RequireHttps] on my BaseController and also a lots of configuration of Application_BeginRequest() in Global.asax.

f.e.:

protected void Application_BeginRequest()
{
    if (!Context.Request.IsSecureConnection)
    {
        // This is an insecure connection, so redirect to the secure version
        UriBuilder uri = new UriBuilder(Context.Request.Url);
        if (!uri.Host.Equals("localhost"))
        {
            uri.Port = 443;
            uri.Scheme = "https";
            Response.Redirect(uri.ToString());
        }
    }
}

Have no idea how to force only HTTPS connection.

Wojna
  • 136
  • 1
  • 17

2 Answers2

6

@PeteG answer was right. IsSecureConnection isnt working all you have to do is to use this code:

      protected void Application_BeginRequest()
    {
        var loadbalancerReceivedSslRequest = string.Equals(Request.Headers["X-Forwarded-Proto"], "https");
        var serverReceivedSslRequest = Request.IsSecureConnection;

        if (loadbalancerReceivedSslRequest || serverReceivedSslRequest) return;

        UriBuilder uri = new UriBuilder(Context.Request.Url);
        if (!uri.Host.Equals("localhost"))
        {
            uri.Port = 443;
            uri.Scheme = "https";
            Response.Redirect(uri.ToString());
        }
    }
Wojna
  • 136
  • 1
  • 17
  • Thanks problem solved. Sad part is within dev tools, and also wen IIS logs of my hosting company I was just getting "site cannot be reached." I had to ask hosting company what they saw on their end and said i keep trying to redirect to https. This brought me here. Thanks much and resolved my issue. – Casey ScriptFu Pharr Jan 22 '20 at 02:12
-2

Because redirection goes in loop. its call same page again and again. like deadlock. Use this Code

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

Or

protected void Application_BeginRequest(Object source, EventArgs e)
{
  if (!Context.Request.IsSecureConnection)
  {
      Response.Redirect(Request.Url.AbsoluteUri.Replace("http://", "https://"));
  }
}
  • yea i know but how to fix it – Wojna Jun 28 '17 at 12:04
  • 1
    What? The OP wants to redirect to HTTPS and your code does exactly the opposite – Camilo Terevinto Jun 28 '17 at 12:10
  • why you people are hurry give negative, i was edit post meanwhile you did negative mark, at least have to wait – Bhargav Mistri Jun 28 '17 at 12:12
  • I liked this response as i was searching this error for ever, and it was your verbiage of the response stating (redirect and redirects same page over over" that made me understand issue. Yes, this community was great when it started and after many years get ego people in it. Your response is helpful also. ;) – Casey ScriptFu Pharr Jan 22 '20 at 02:10