Hi, I want to implement an ISAPI filter like feature using HttpModule in IIS7 running under IIS Integrated Request Processing Pipeline mode.

The goal is to look at the incoming request at the Web Server level, and inject some custom HttpHeaders into the request. (for ex: HTTP_EAUTH_ID)

And later in the page lifecycle of an ASPX page, i should be able to use that variable as

string eauthId = Request.ServerVariables["HTTP_EAUTH_ID"].ToString();

So implementing this module at the Web Server level, is it possible to alter the ServerVariables collection ??



I believe the server variables list only contains the headers sent from the browser to the server.

Joel Martinez
+2  A: 

HttpRequest.ServerVariables Property is a read-only collection. So, you cannot directly modify that. I would suggest storing your custom data in httpcontext (or global application object or your database) from your httpmodule and then reading that shared value in the aspx page.

If you still want to modify server variables, there is a hack technique mentioned in this thread using Reflection.


You won't be able to modify either the HttpRequest.Headers or the HttpRequest.ServerVariables collection. You will however be able to tack on your information to any of:


Unfortunately, Request.Params, Request.QueryString, Request.Cookies, Request.Form (and almost any other place you'd think of stuffing it is read only.

I'd strongly advise against using reflection if this is a HttpModule you're planning on installing into IIS 7. Given that this code will be called for (potentially) every request that goes through the web server it'll need to be really fast and reflection just isn't going to cut it (unless you have very few users).

Good luck!