Click here to Skip to main content
6,634,665 members and growing! (16,193 online)
Email Password   helpLost your password?
Web Development » Web Security » Security     Advanced

Simple HTTP Reverse Proxy with ASP.NET and IIS

By Vincent Brossier

Learn how easy it is to create HTTP Reverse Proxy in .NET using IIS.
C#.NET 1.1, Win2K, WinXP, Win2003, ASP.NET, Visual Studio, Dev
Posted:22 May 2004
Views:205,555
Bookmarked:71 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
24 votes for this article.
Popularity: 5.41 Rating: 3.92 out of 5
2 votes, 8.7%
1

2
3 votes, 13.0%
3
7 votes, 30.4%
4
11 votes, 47.8%
5

Introduction

Sample Image - reverse-proxy.png

A reverse proxy is the same as a proxy except instead of delivering pages for internal users, it delivers them for external users. It can be used to take some load off web servers and provide an additional layer of protection. If you have a content server that has sensitive information that must remain secure, you can set up a proxy outside the firewall as a stand-in for your content server. When outside clients try to access the content server, they are sent to the proxy server instead. The real content resides on your content server, safely inside the firewall. The proxy server resides outside the firewall, and appears to the client to be the content server.

Where to use?

If you have an intranet with IP filtered security, you can offer the functionality of external consultation. You must just add the authentication system of your choice.

Functionality

This reverse proxy can run in two modes:

  • Mode 0: all web sites can be requested by clients with an URL like http://RevrseProxyURL/http//www.site.com/
  • Mode 1: only one web site can be requested. When the user requests the web application address, the proxy returns the content of this web site.

You can setup this mode in the web configuration file of you web application:

<appSettings>
    <!-- PROXY Mode
  0 : all web site can be requested by a client with 
      an url like http://ReverseProxyURL/http//www.site.com/
  1 : Only on web site can be resuested by clients, \
      In this case Web Application uses RemoteWebSite variable 
      to deliver the content of the web site.
    -->
   <add key="ProxyMode" value="1" />
   <add key="RemoteWebSite" value="http://www.codeproject.com/">http://www.codeproject.com/" />
</appSettings>

The code

Create an HttpHandler to intercept all requests:

using System;
using System.Configuration;
using System.Web;
using System.Net;
using System.Text;
using System.IO; 
namespace ReverseProxy
{
  /// <summary>

  /// Handler that intercept Client's request and deliver the web site

  /// </summary>


  public class ReverseProxy: IHttpHandler
  {
    /// <summary>

    /// Method calls when client request the server

    /// </summary>

    /// &;lt;param name="context">HTTP context for client</param>


    public void ProcessRequest(HttpContext context)
    {
      //read values from configuration 

      fileint proxyMode = 
        Convert.ToInt32(ConfigurationSettings.AppSettings["ProxyMode"]);
      string remoteWebSite = 
        ConfigurationSettings.AppSettings["RemoteWebSite"];
      string remoteUrl;
      if (proxyMode==0)
        remoteUrl= ParseURL(context.Request.Url.AbsoluteUri);
      //all site 

      acceptedelseremoteUrl= 
        context.Request.Url.AbsoluteUri.Replace("http://"+
        context.Request.Url.Host+
        context.Request.ApplicationPath,remoteWebSite);
      //only one site accepted

      //create the web request to get the remote stream

      HttpWebRequest request = 
        (HttpWebRequest)WebRequest.Create(remoteUrl);
      //TODO : you can add your own credentials system

      //request.Credentials = CredentialCache.DefaultCredentials;

      HttpWebResponse response;
      try
      {
        response = (HttpWebResponse)request.GetResponse ();
      }
      catch(System.Net.WebException we)
      {
        //remote url not found, send 404 to client 

        context.Response.StatusCode = 404;
        context.Response.StatusDescription = "Not Found";
        context.Response.Write("<h2>Page not found</h2>");
        context.Response.End();
        return;
      }
      Stream receiveStream = response.GetResponseStream();

      if ((response.ContentType.ToLower().IndexOf("html")>=0) 
        ||(response.ContentType.ToLower().IndexOf("javascript")>=0))
      {
        //this response is HTML Content, so we must parse it

        StreamReader readStream = 
          new StreamReader (receiveStream, Encoding.Default);
        Uri test = new Uri(remoteUrl);
        string content;
        if (proxyMode==0)
          content= ParseHtmlResponse(readStream.ReadToEnd(), 
            context.Request.ApplicationPath+"/http//"+test.Host);
        else
          content= ParseHtmlResponse(readStream.ReadToEnd(),
            context.Request.ApplicationPath);
        //write the updated HTML to the client

        context.Response.Write(content);
        //close streamsreadStream.Close();

        response.Close();
        context.Response.End();
      }
      else
      {
        //the response is not HTML 

        Contentbyte[] buff = new byte[1024];
        int bytes = 0;
        while( ( bytes = receiveStream.Read( buff, 0, 1024 ) ) > 0 )
        {
          //Write the stream directly to the client 

          context.Response.OutputStream.Write (buff, 0, bytes );
        }
        //close streams

        response.Close();
        context.Response.End();
      }
    }

    /// <summary>

    /// Get the remote URL to call

    /// </summary>

    /// <param name="url">URL get by client</param>

    /// <returns>Remote URL to return to the client</returns>


    public string ParseURL(string url)
    {
      if (url.IndexOf("http/")>=0)
      {
        string externalUrl=url.Substring(url.IndexOf("http/"));
        return externalUrl.Replace("http/","http://") ;
      }
      else
        return url;
    }

    /// <summary>

    /// Parse HTML response for update links and images sources

    /// </summary>

    /// <param name="html">HTML response</param>

    /// <param name="appPath">Path of application for replacement</param>

    /// <returns>HTML updated</returns>

    public string ParseHtmlResponse(string html,string appPath)
    {
      html=html.Replace("\"/","\""+appPath+"/");
      html=html.Replace("'/","'"+appPath+"/");
      html=html.Replace("=/","="+appPath+"/");
      return html;
    }
    ///

    /// Specifies whether this instance is reusable by other Http requests

    ///

    public bool IsReusable
    {
      get
      {
        return true;
      }
    }
  }
}

Configure the handler in web.config

You must add these lines in web.config file to redirect all user queries to the HTTPHandler:

<httpHandlers>
   <add verb="*" path="*" type="ReverseProxy.ReverseProxy, ReverseProxy" />
</httpHandlers>

Configure IIS

If you want to process a request with any file extension, then you need to change IIS to pass all requests through to the ASP.NET ISAPI extension. Add the HEAD, GET and POST verbs to all files with .* file extension and map those to the ASP.NET ISAPI extension - aspnet_isapi.dll (in your .NET framework directory). The complete range of mappings, includes the new .* mapping.

TODO's

Now, you can develop your own security system, based on form, Windows or passport authentication.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vincent Brossier


Member
Vincent Brossier is a software engineer from Paris, FRANCE, specializing in .Net.
Vincent is in charge of development of the Parisian university's Intranet, I take part in a nationnal project of numerical campus.
His role is to seek best technicals solutions to satisfy the 35000 users of this Intranet. Specialized in the development of distributed applications, vincent work primarily with technologies microsoft .NET (C#, ASP.NET, Webservices, SQLServer) even if he have also work with java technologies (Peer-to-peer sharing documents tool).
Vincent have also set up Open-Source solutions such as SPIP in nursery schools.
When he's not programming, he enjoys playing Guitar.
Occupation: Web Developer
Location: France France

Other popular Web Security articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 74 (Total in Forum: 74) (Refresh)FirstPrevNext
GeneralHow to set it up? Pinmembermaria_mir21:48 7 Jul '09  
GeneralRe: How to set it up? Pinmembermaria_mir23:18 12 Jul '09  
GeneralSome changes Pinmemberrickleb7:16 21 Mar '09  
GeneralRe: Some changes PinmemberMunirS3:04 30 Aug '09  
GeneralPour Infos PinmemberMember 21014414:34 24 Sep '08  
GeneralInstallation Instructions PinmemberChris Spencer4:17 15 Jul '08  
GeneralRegarding Host Header Pinmemberpmselvan_20009:29 5 Jul '08  
GeneralRe: Regarding Host Header PinmemberMunirS2:56 30 Aug '09  
GeneralError PinmemberVJLaks21:39 16 Jun '08  
GeneralDerivative Work PinmemberPaul Johnston5:35 4 May '08  
GeneralErrors Pinmemberhanisacsouka3:32 13 Apr '08  
Generalhttp://www.site.com:8080/ Pinmemberhanisacsouka9:05 12 Apr '08  
GeneralIIS 7.0 Pinmemberbrice2nice11:50 13 Nov '07  
Generalenabling file download Pinmembertarak4v20:49 5 Sep '07  
GeneralVery usefull for me. View my project for download files from rapidshare.com based on this sample [modified] PinmemberW.M. Spon23:54 29 Jun '07  
GeneralI couldnt get it work Pinmemberjeanmoura6:01 12 Apr '07  
Questionimage path doesnt work Pinmembernolovelust11:45 5 Apr '07  
AnswerRe: image path doesnt work PinmemberMySuperName22:51 11 Jan '09  
GeneralBisuits Pinmembersafari750125:52 23 Mar '07  
GeneralHTTPS support PinmemberDeason19:11 21 Nov '06  
GeneralReverse proxy apache and IIS Pinmemberharinath3:29 28 Oct '06  
General401 error Pinmembermredlon3:18 14 Sep '06  
GeneralReverse proxy with .css and .aspx files. Pinmemberyashchawhan20:40 3 Aug '06  
GeneralRe: Reverse proxy with .css and .aspx files. PinmemberMunirS2:39 30 Aug '09  
QuestionReverse Proxy Pinmemberyashchawhan3:45 24 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 May 2004
Editor: Smitha Vijayan
Copyright 2004 by Vincent Brossier
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project