Click here to Skip to main content
15,888,035 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello,

I am facing redirection issue for the HTTPS url. I have SSL certificate installed at the server. Please find my redirection code below. I have placed it at Application_BeginRequest event of global.asax file.

VB
If Not HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://") Then
            If ConfigurationManager.AppSettings("UseSecure").ToLower() = "true" Then
                Dim securePages As String = ConfigurationManager.AppSettings("SecurePages")
                Dim securePagesList As String() = securePages.Split(",")
                Dim IsSSLPage = False
                For Each page In securePagesList
                    If strUrl.IndexOf(page) <> -1 Then
                        If strUrl.IndexOf(".aspx") > 0 And HttpContext.Current.Request.ServerVariables("HTTPS") = "off" Then                           
                            IsSSLPage = True
                            Exit For
                        End If
                    End If
                Next
                Common.RedirectWithSSL(IsSSLPage)
            End If
        End If


VB
Public Shared Sub RedirectWithSSL(ByVal isSSL As Boolean)
       If HttpContext.Current.Request.HttpMethod.ToLower() = "post" Then
           Return
       End If
       Dim httpurl As [String] = System.Web.HttpContext.Current.Request.Url.ToString()
       Dim checkUrl As String = httpurl.ToLower()
       If isSSL And httpurl.ToLower().StartsWith("http://") AndAlso Not httpurl.ToLower().StartsWith("https://") Then
         
               httpurl = httpurl.Replace("http", "https").ToString()
               System.Web.HttpContext.Current.Response.Redirect(httpurl)
         
       ElseIf Not isSSL And checkUrl.StartsWith("https://") Then
           httpurl = httpurl.Replace("https", "http").ToString()
           System.Web.HttpContext.Current.Response.Redirect(httpurl)
       End If
   End Sub


I have saved my secure page name in web.config file with comma separated like checkout,meber,orderhistory etc.

Now, when I check the URL like https://mysite.com/member/ it fires 302 status with redirecting loop. While I check it with https://mysite.com/member.aspx it's working just fine.

I cannot figure out the issue. It is only issue at server. In my development machine it is working just fine. (I have installed self signed certificate with the help of http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates.aspx[^]
Posted

1 solution

Finally after spending 15 hours on this, I finally found the solution. :omg:

The issue is my hosting company use a load balancer on their server so the If Not HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://") is never got set to true. So for every request it goes to that function and redirection loop occurs.

In a Load Balanced environment the SSL connection stops at the load balancer so that you can never test for an SSL connection using standard methods such as:
- HttpContext.Current.Request.IsSecureConnection
- HttpContext.Current.Request.Url.Scheme
- Request.ServerVariables("HTTPS")
- HttpContext.Current.Request.Url.AbsoluteUri.Contains("https://")

In a load balanced environment a custom request gets inserted into the header to assist in identifying a secure request. Assuming that your host has setup this variable on their load balancer its name could be anything. In the case of my hosting server it appears to be "HTTP_CLUSTER_HTTPS". X|

I hope this solution will save some one else 15 hours. :)
 
Share this answer
 
Comments
Ankur\m/ 18-Jan-11 0:37am    
Let me give you some points for your efforts! :)
That's Aragon 18-Jan-11 0:49am    
Thanks Ankur..
Evanwb 14-Jul-12 12:22pm    
I have a similar situation with a load balanced environment and found your solution via a Google search. I wouldn’t have thought to look for a custom request header. Thanks for the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900