Question

I have an HttpModule that I created and am running on IIS 6 using Windows Server 2003. I can send cookies to the browser, but I can not read them on the next request, cookie is always null.

If I run this module on IIS 7 though, it works just fine. IIS 7 not an option at the moment as we have not switched over and this needs to get done before that will happen.

Also, I've already tried using the PostAcquireRequestState hook.

    public void Init(HttpApplication httpApp)
    {
        httpApp.BeginRequest += OnBeginRequest;
    }

    public void OnBeginRequest(Object sender, EventArgs e)
    {
        var httpApp = (HttpApplication)sender;
        var context = httpApp.Context;

        const string cookieName = "sId";

        if (!string.IsNullOrEmpty(context.Request.QueryString["cookie"]))
        {
            var ck = new HttpCookie(cookieName)
                            {
                                Value = httpApp.Context.Request.QueryString["cookie"],
                                Expires = DateTime.Now.AddDays(1)
                            };

            httpApp.Response.Cookies.Add(ck);
        }
        else
        {
            var cookie = httpApp.Request.Cookies[cookieName]
        }
    }
Was it helpful?

Solution

your code look worked.the problem may be occur in the client-side how to request the next page.you can use the firebug with firefox or the fidder tools that can log your client-side request and see the request whether send cookd value in the request header to the server.

for example

the request headers:

get /1.aspx
.....
Cookie: sId=123 [if the client has a cookie then it will appear in here.] 

the response headers:

Set-Cookie: sId=123; expires=Fri, 30-Mar-2012 07:20:23 GMT; 
path=/

if the server add cookie to the response,then response it look like the above.

now,i guess the problem in your cook domain or you cookie path is different.

the best method to set cookie is like the follow code:

var ck = new HttpCookie(cookieName)
{
   Value = httpApp.Context.Request.QueryString["cookie"],
   Expires = DateTime.Now.AddDays(1),
   Path="/",
   Domain="your domain"
};

good luck.

OTHER TIPS

I ran into a similar problem, but had a different solution, so I thought I'd share, in case it helps someone. I took zengchun's suggestion as well to use some tools to inspect request & response headers. Since I'm using IE, the F12 Dev Tools works great for this. As soon as I saw the response header for the cookie, I noticed the secure flag was set. Sure enough, I had copied code from a production SSL-hosted site to a test site that did not use SSL, so the secure flag on the cookie prevented the code from being able to read it. I updated the web.config to remove requireSSL from the httpcookies node, and my site started working. :)

Thanks to zhengchun I was able to get to the root of the problem. It turns out I was unable to set the cookie using requests to static files. I created .aspx files for my initial requests that redirected to the static files after setting the cookie. My HttpModule could then read the cookie after being set in the .aspx file. Not sure why I need a .aspx file to set the cookie instead of the HttpModule, but this fixed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top