Wednesday, November 15, 2006

I actually hated coding for web. But, then I was forced to do that. This is because; we need to worry about a lot of problems unnecessarily. This includes browser capability, IIS configurations, permission problems etc. So I will be using this space to talk about these things:

Today I am going to talk about a problem that came across me:

In one of our project we had a problem where we need to change the Request Cookie in asp.net pages. This was a big task for us since there were around 100 pages and this has to be completed in less time. So modifying every page looked out of question.

Then I thought of using HttpModule to handle the situation by modifying the Request cookie.

After implementing this I realized a shocking factor about Request.Cookies. Whenever we modify the Respose.Cookies by removing an existing cookie, the Request.Cookies collection is reset to the original values. This means all the changes that we did to the response cookie is lost.

It was a nightmare for me to solve this issue. But fortunately I tried a total differnt way of solving this issue. I used Reflection to change the Response.Cookies collection object to a new CookieCollection and added all the Request.Cookies to it. All this happens in the BeginRequest event handler.

This way whenever the Response cookies are modified within the code anywhere else, the request cookies are not reset.

Here is the code snippet:

// In the Init function, register for HttpApplication
// events by adding your handlers.
public void Init(HttpApplication application)
{
application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
application.EndRequest += (new EventHandler(this.Application_EndRequest));
}
// Your BeginRequest event handler.
private void Application_BeginRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
ChangeRequest(context);
if(context.Request.Cookies["Context_Encr"]!= null )
{
HttpCookie cookie = HttpCookieEncryption.Decrypt(context.Request.Cookies["Context_Encr"]);
context.Request.Cookies.Remove("Context" );
cookie.Name = "Context";
context.Request.Cookies.Add(cookie);
}
}
catch
{
throw;
}
}

private void ChangeRequest(HttpContext context)
{
Type t = context.Response.GetType();
HttpCookieCollection col = new HttpCookieCollection();
for(int i =0;i< context.Request.Cookies.Count;i++)
{
HttpCookie c = HttpCookieEncryption.CloneCookie(context.Request.Cookies[i]);
col.Add(c);
}

//Just get the cookie collection and create a new Collection and assign to it
FieldInfo info = t.GetField("_cookies",BindingFlags.NonPublic BindingFlags.Instance);
if(info != null)
{
info.SetValue(context.Response,col);
}
}

// Your EndRequest event handler.
private void Application_EndRequest(Object source, EventArgs e)
{
HttpApplication application = (HttpApplication)source;
HttpContext context = application.Context;
try
{
if(context.Response.Cookies["Context"]!= null )
{
HttpCookie cookie = HttpCookieEncryption.Encrypt(context.Response.Cookies["Context"]);
context.Response.Cookies.Remove("Context");
cookie.Name = "Context_Encr";
context.Response.Cookies.Set(cookie);
}
}
catch
{
throw;
}
finally
{
context.Response.Cookies.Remove("Context");
}
}

I think this helps somebody facing the same music ;)

1 Comments:

At 9:26 PM, Anonymous Anonymous said...

I think everything said was very reasonable.
But, what about this? suppose you typed a catchier post title?
I am not suggesting your content is not good., however what if
you added something that grabbed people's attention? I mean Untitled
is a little plain. You might peek at Yahoo's front page and watch how they create news headlines to grab people interested.

You might add a related video or a related pic or two to get people excited about what you've written. Just my opinion, it would bring your posts a little bit more interesting.

 

Post a Comment

<< Home