How to add Custom headers into readonly httprequest object using httpModule

Dhirendra
Posted by in ASP.NET category on for Intermediate level | Points: 250 | Views : 12250 red flag

The asp.net httprequest object provides information about the current http request made by the client browsers. So in this article, we will learn how we can add custom header into httprequest object for each aspx request even if this object is read only.

Introduction

The asp.net HttpRequest object provides information about the current http request made by the client browsers. So in this article, we will learn how we can add custom header into HttpRequest object for each aspx request even if this object is read only. In asp.net, every request made by the client browser goes through the asp.net application life cycle. In this life cycle there are several modules invokes to process the request after the iis handover the request to asp.net engine/asp.net ApplicationManager object.

After the asp.net engine start processing the request, the asp.net hosting environment creates HttpContext object of that asp.net application. After that the Http module is invokes for each request. The http module calls before and after the HttpHandler executes. so we can take advantage of http module to intercept the HttpHandler's request object and add custom header for each asp.net HttpRequest

 

HTTPModule Impletation

Create httpmodule by implementing IHttpModule interface

public class CustomeModule : IHttpModule

{

public void Dispose()

{

//clean-up code here.

}

public void Init(HttpApplication context)

{

context.BeginRequest += new EventHandler(context_BeginRequest);

}

void context_BeginRequest(object sender, EventArgs e)

{

var headers = HttpContext.Current.Request.Headers;

Type t = headers.GetType();

PropertyInfo prop = t.GetProperty("IsReadOnly"BindingFlags.Instance | BindingFlags.NonPublic |BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy);

prop.SetValue(headers, falsenull);

t.InvokeMember("BaseAdd"BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, headers, new object[] { "CustomHeaderKey"new ArrayList { "Your CustomHeader Value of request object" } });

prop.SetValue(headers, truenull);

}

}

add below web.config setting under <system.web> configuration setting

<httpModules >

<add name="CustomeModule" type="WebApplication70515.CustomeModule"/>

</httpModules>

ON page load event of the page see if the custome header value is available

here is the code of aspx.cs page

public partial class CustomHeaderTest : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

if (Request.Headers["CustomHeaderKey"] != null)

{

Response.Write(Request.Headers["CustomHeaderKey"]);

}

}

}

Conclusion

In the above article, we saw that even if the HttpRequest object is read only, still we can add custom headers for each asp.net request through httpmodule interceptor.

Page copy protected against web site content infringement by Copyscape

About the Author

Dhirendra
Full Name: Dhirendra Patil
Member Level:
Member Status: Member
Member Since: 3/23/2010 2:39:20 PM
Country: India

http://www.dotnetfunda.com

Login to vote for this post.

Comments or Responses

Posted by: Koenigsegg on: 10/2/2012 | Points: 25
Thank you for your post Dhirendra it was really helpful for me. Following your example I have managed to successfully set a variable in the HTTP request header. I now need to pass this variable in the HTTP request header to a 3rd party website but I can't work out how to do it. Server.Transfer(url) can't be used as this is only for transfers on the same server and if I use Response.Redirect(url) it clears the HTTP request header values.

Any advice would be gratefully received as I am stuck on this at the moment.

Login to post response

Comment using Facebook(Author doesn't get notification)