IIS7 - http2https

Here is the explanation of how you can write a simple HTTP to HTTPS redirection using very few lines of code...

So let us get into action (I'm using C# for this sample)

  1. Download and Install IIS7 Managed Module Starter Kit
    (Not really a requirement but it would make developing IIS7 modules easier)
  2. Rename the default class name created to "redir.cs" and rename project/solution/namespace to "http2https"
  3. Add the following code in "Init" method
    // register for the BeginRequest event
    application.BeginRequest += new EventHandler(OnBeginRequest); 
    application.EndRequest += new EventHandler(OnEndRequest);
  4. Add the following method to implement "BeginRequest" event
    //BeginRequest implementation
    public void OnBeginRequest(Object sender, EventArgs e)
    {
    HttpApplication app = (HttpApplication)sender;
    string HttpUrl = app.Request.Url.ToString(); 
    
       if (HttpUrl.StartsWith("http:"))                           //Redirection done only if URL starts with http:
       {
       HttpUrl = HttpUrl.Replace("http:", "https:");
       app.Response.Redirect(HttpUrl.ToString(), true);           //Redirecting (http 302) to the same URL but with https
       app.Response.End();                                        //We don't want to go any further so end
       }
    } 
    
  5. Add the following method to implement "OnEndRequest" event

    //This is for scenario where SSL is forced on the site
    public void OnEndRequest(Object sender, EventArgs e)
    {
      HttpApplication app = (HttpApplication)sender;
      if (app.Response.StatusCode == 403 && app.Response.SubStatusCode == 4)
      { 
        string HttpUrl = app.Request.Url.ToString();
    
        if (HttpUrl.StartsWith("http:"))
        {
            HttpUrl = HttpUrl.Replace("http:", "https:");
            app.Response.Redirect(HttpUrl.ToString(), true);
            app.Response.End();
        }
    }
    
    

  6. Make sure you have the following in your web.config inside configuration tag
    <system.webServer>
    <modules>
       <add name="redir" type="http2https.redir" />
    </modules>
    </system.webServer> 
    

 

Your http to https redirection sample is ready and also works if you force SSL!!!


How to deploy the HttpModule
There are multiple ways you can deploy this component (I'm assuming that it's being deployed for "default website")

Method 1
Create a folder called "App_Code" inside "%systemdrive%\inetpub\wwwroot"
Copy "redir.cs" file into "App_Code" folder
Copy "web.config" file inside "%systemdrive%\inetpub\wwwroot"

Method 2
Create a folder called "bin" inside "%systemdrive%\inetpub\wwwroot"
Compile "redir.cs" into "redir.dll" and copy it into "bin" folder (to compile -> csc.exe /out:redir.dll /target:library redir.cs)
Copy "web.config" file inside "%systemdrive%\inetpub\wwwroot"

If you open IIS7 UI and go to Modules you can see your HttpModule listed there.

Download Soure and Binary
http2https.zip (195.68 kb)

 

Author

Sukesh Sukesh Ashok Kumar
Works @ Microsoft
More...

Chat with me!
who's online

Disclaimer

All opinions posted here are those of the author and are in no way intended to represent the opinions of his employer. All posts are provided "AS IS" with no warranties, and confers no rights. © Copyright 2010

Recent Comments

Comment RSS

Sign in