Click here to Skip to main content
13,351,465 members (49,258 online)
Click here to Skip to main content
Add your own
alternative version


88 bookmarked
Posted 22 May 2004

Simple HTTP Reverse Proxy with ASP.NET and IIS

, 22 May 2004
Rate this:
Please Sign up or sign in to vote.
Learn how easy it is to create HTTP Reverse Proxy in .NET using IIS.


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.


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//
  • 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:

    <!-- PROXY Mode
  0 : all web site can be requested by a client with 
      an url like <A href="http://reverseproxyurl/http//">http://ReverseProxyURL/http//</A>
  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="<A href=""></A>" />

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 = 
      string remoteWebSite = 
      string remoteUrl;
      if (proxyMode==0)
        remoteUrl= ParseURL(context.Request.Url.AbsoluteUri);
      //all site 
      //only one site accepted
      //create the web request to get the remote stream
      HttpWebRequest request = 
      //TODO : you can add your own credentials system
      //request.Credentials = CredentialCache.DefaultCredentials;
      HttpWebResponse response;
        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>");
      Stream receiveStream = response.GetResponseStream();

      if ((response.ContentType.ToLower().IndexOf("html")>=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(), 
          content= ParseHtmlResponse(readStream.ReadToEnd(),
        //write the updated HTML to the client
        //close streamsreadStream.Close();
        //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

    /// <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://") ;
        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)
      return html;
    /// Specifies whether this instance is reusable by other Http requests
    public bool IsReusable
        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:

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

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.


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


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
Web Developer
France France
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.

You may also be interested in...

Comments and Discussions

GeneralPass values from .aspx page to remote URL that contains an asp page Pin
Anonymous18-Oct-05 21:10
sussAnonymous18-Oct-05 21:10 
GeneralW2k3 / IIS 6 Pin
David Llamas15-Jul-05 18:44
memberDavid Llamas15-Jul-05 18:44 
AnswerRe: W2k3 / IIS 6 Pin
tarak4v9-Sep-07 23:10
membertarak4v9-Sep-07 23:10 
GeneralAccess Denied... Pin
Ritchie Carroll28-Jun-05 5:03
memberRitchie Carroll28-Jun-05 5:03 
GeneralRe: Access Denied... Pin
Ritchie Carroll29-Jun-05 11:59
memberRitchie Carroll29-Jun-05 11:59 
GeneralUnit Testing ReverseProxy Pin
btakita25-May-05 12:49
memberbtakita25-May-05 12:49 
GeneralCant see images Pin
pratz1728-Apr-05 4:08
memberpratz1728-Apr-05 4:08 
GeneralRe: Cant see images Pin
Ritchie Carroll28-Jun-05 4:57
memberRitchie Carroll28-Jun-05 4:57 
Try increasing the default connection limit - it's set to 2 by default - not enough for a web page with many graphics:

ServicePointManager.DefaultConnectionLimit = 100

Hope that helps!
JokeRe: Cant see images Pin
UL-Tomten16-Jan-08 8:56
memberUL-Tomten16-Jan-08 8:56 
GeneralAdding Session Handling Pin
David Osbourn10-Mar-05 7:01
memberDavid Osbourn10-Mar-05 7:01 
GeneralRe: Adding Session Handling Pin
Vincent Brossier10-Mar-05 9:51
memberVincent Brossier10-Mar-05 9:51 
GeneralRe: Adding Session Handling Pin
David Osbourn10-Mar-05 23:53
memberDavid Osbourn10-Mar-05 23:53 
GeneralRe: Adding Session Handling Pin
Yu B.R6-May-05 20:49
memberYu B.R6-May-05 20:49 
GeneralReturning PDF Pin
Chris A.7-Mar-05 12:30
memberChris A.7-Mar-05 12:30 
GeneralRe: Returning PDF Pin
Vincent Brossier7-Mar-05 20:45
memberVincent Brossier7-Mar-05 20:45 
Generalplease help the newbie Pin
Marc-O19-Jan-05 6:24
sussMarc-O19-Jan-05 6:24 
GeneralRe: please help the newbie Pin
Kokamo16-Apr-05 18:18
memberKokamo16-Apr-05 18:18 
GeneralRequesting a file in a different folder Pin
yfirst19-Jan-05 3:27
memberyfirst19-Jan-05 3:27 
GeneralCould not load type 'ReverseProxy.Global'. Pin
webwidejosh14-Dec-04 7:04
memberwebwidejosh14-Dec-04 7:04 
Generalvery simple... Pin
gomolyako24-Oct-04 10:58
sussgomolyako24-Oct-04 10:58 
GeneralVisual Studio problem Pin
oferws15-Jun-04 22:32
sussoferws15-Jun-04 22:32 
GeneralRe: Visual Studio problem Pin
Anonymous20-Jun-04 8:06
sussAnonymous20-Jun-04 8:06 
GeneralHTTP post method Pin
oferws6-Jun-04 1:20
memberoferws6-Jun-04 1:20 
GeneralRe: HTTP post method Pin
gomolyako24-Oct-04 10:53
sussgomolyako24-Oct-04 10:53 
GeneralRe: HTTP post method Pin
pacman189115-Dec-04 23:10
memberpacman189115-Dec-04 23:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180111.1 | Last Updated 23 May 2004
Article Copyright 2004 by Vincent Brossier
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid