Click here to Skip to main content
15,886,832 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTTPS redirection not working using Global.asax.
My code is as follows:
ASP.NET
<%@ Application Language="C#" %>

<script runat="server">

    //Added by Alex on Jan 29,2014 to clear http/https issue  without enabling SSL Certificate
    private void RedirectToCorrectSSLScheme()
    {
        Uri pageRequest = Request.Url;
        string requestPath = pageRequest.GetLeftPart(UriPartial.Path).ToLower();

         requestPath = Server.UrlDecode(requestPath);
        // PageIsSecure returns if the given page should be secure or not. I 
        //maintain a list of secure pages or 
        //secure directory in an XML config. 
      //  bool securePage = GetSecurePages().PageIsSecure(requestPath);
        //Instead of localhost we can set the IP Address of Qatar server
        if (pageRequest.Scheme == "https" && pageRequest.Host.Contains("localhost") && requestPath.Contains(".aspx"))
        {
            Response.Redirect("http://" + pageRequest.Host + pageRequest.PathAndQuery, true);
           // Response.Redirect(requestPath, true);
        }
        //Added by Alex to check  external url of qatar server
        //Instead of localhost we can set the IP Address of Qatar server
        if (pageRequest.Scheme == "http" && pageRequest.Host.ToString()!="localhost"  && requestPath.Contains(".aspx"))
       
        {
           Response.Redirect("https://" + pageRequest.Host + pageRequest.PathAndQuery, true);

           //Response.Redirect(requestPath, true); 
        }
    }
    void  Application_BeginRequest(object sender, EventArgs e)
    {
        //Function Added by Alex on Jan 29,2014 to clear http/https issue under Qatar Server
        RedirectToCorrectSSLScheme();

    }

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

    }
  
    
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
       
</script>

It isn't changing from http to https in the url even though its entering the if loop properly. Its still remaining as http://localhost/.....
Posted
v2
Comments
Bernhard Hiller 29-Jan-14 9:08am    
//Added by Alex on Jan 29,2014 to clear http/https issue without enabling SSL Certificate
What the ...? You cannot simply replace http by https and vice versa!
Kornfeld Eliyahu Peter 29-Jan-14 9:23am    
No IIS will override it. https to http if no SSL enabled or http to https if SSL enforced. In case that SSL enabled but not enforced you may do as you wish...
Ahmed Bensaid 29-Jan-14 9:27am    
Debug your code ... I think it won't pass into your if conditions if you test with "http://localhost/" ...
Rahul 105 29-Jan-14 22:52pm    
Actualy I enabled ssl and not enforced the same. Then did as follows:

if (pageRequest.Scheme == "http" && pageRequest.Host.ToString().Contains("localhost") && requestPath.Contains(".aspx"))

{

Response.Redirect("https://" + pageRequest.Host + pageRequest.PathAndQuery, true);

}
But http not changing to https in local url.

1 solution

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