2

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]
        }
    }
James
  • 680
  • 2
  • 8
  • 22

3 Answers3

4

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. :)

VG10
  • 41
  • 2
1

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.

zhengchun
  • 1,261
  • 13
  • 19
  • Thanks a lot for the help. This led me to the real root of the problem, which was I can't send cookies over static file requests. My original request needs to be an .asp or .aspx file in order to set the cookie, then my HttpModule can read the cookies as needed. Sorry for the bit of miss information in the beginning, I could've sworn the server was actually reading the cookie, lol. – James Mar 29 '12 at 14:09
  • you are welcome,i know why you sad the application running IIS7 is okay.because you use integrated pool in the IIS7 . – zhengchun Mar 30 '12 at 01:51
0

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.

James
  • 680
  • 2
  • 8
  • 22