Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Building Controls / September 2006

Tip: Looking for answers? Try searching our database.

HttpModule Cookie Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Eric Marthinsen - 26 Sep 2006 08:28 GMT
Hello-

I wrote a HttpModule to inspect the user's current page and to assign a
source a content category to them. The problem is that when it runs, it wipes
out users cookies which logs them out of the site and wipes out their basket.
I can't recreate the error in any sort of test or staging environment, so I'm
at a complete loss as to what might be causing this. Any ideas? Here's the
important bit of my code:

#region IHttpModule Members

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(OnBeginRequest);
}

public void Dispose()
{
    // TODO:  Add CategoryManager.Dispose implementation
}

#endregion

private void OnBeginRequest(object sender, EventArgs e)
{
    try
    {
        //get the http application
        HttpApplication context = (HttpApplication)sender;

        string pageID = GetPageID(context);
        string categoryID = GetCategoryID(context);
        string source = GetSource(context);

        //add the info that can be preserved to the user's cookie
        HttpCookie categoryCookie = new HttpCookie("cmCategory", categoryID);
        context.Response.Cookies.Add(categoryCookie);

        HttpCookie sourceCookie = new HttpCookie("cmSource", source);
        context.Response.Cookies.Add(sourceCookie);

        //add all values to HttpContext.Items for the actual tags to use
        TaggingSession.PageID = pageID;
        TaggingSession.CategoryID = string.Format("{0}:{1}", source, categoryID);
//create the composite category with the source and category
    }
    catch(Exception ex)
    {
        ExceptionManager.Publish(ex);
    }
}

OnBeginRequest calls a few helper functions (GetSource, GetCategory, etc.).
These methods only do reads and inspect the Request.Cookie collection, the
current URL (for querystrings) and an XML file (to look up page IDs). I can
post that as well, if nevessary, but it's a lot of code to go through.

Regards-
    Eric
Gaurav Vaish (www.EduJiniOnline.com) - 26 Sep 2006 18:49 GMT
> //add the info that can be preserved to the user's cookie
> HttpCookie categoryCookie = new HttpCookie("cmCategory", categoryID);
> context.Response.Cookies.Add(categoryCookie);
>
> HttpCookie sourceCookie = new HttpCookie("cmSource", source);
> context.Response.Cookies.Add(sourceCookie);

Strange problem... try tracing the cookies before and after adding.

Have the following code before and after adding the cookies.
btw, have you checked the list of cookies at the client (or the raw response
from the server)?

foreach(HttpCookie cookie in context.Response.Cookies)
{
   Trace.Write(...)
}

Signature

Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://www.edujinionline.com
http://articles.edujinionline.com/webservices
-------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.