Search code examples
asp.netcookiesiis-6httpmoduleihttpmodule

HttpModule not receiving cookies on IIS 6


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]
        }
    }

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.