HTTP Module and Cookies in Sharepoint 2007
I have some proof concept code for a HTTP module. The code checks to see if a cookie exists, if so it retrieves a value, if the cookie does not exist it creates it and sets the value.

Once this is done I write to the screen to see what action has been taken (all nice and simple). So on the first request the cookie is created; subsequent requests retrieve the value from the cookie. 

When I test this in a normal asp.net web site everything works correctly – yay! However as soon as I transfer it to SharePoint something weird happens, the cookie is never saved - that is the code always branches into creating the cookie and never takes the branch to retrieve the value - regardless of page refreshes or secondary requests.

Heres the code...

    public class SwithcMasterPage : IHttpModule
    {       

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            // register handler
            context.PreRequestHandlerExecute += new EventHandler(PreRequestHandlerExecute);
        }

        void PreRequestHandlerExecute(object sender, EventArgs e)
        {
            string outputText = string.Empty;

            HttpCookie cookie = null;
            string cookieName = "MPSetting";

            cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                // cookie doesn't exist, create
                HttpCookie ck = new HttpCookie(cookieName);
                ck.Value = GetCorrectMasterPage();
                ck.Expires = DateTime.Now.AddMinutes(5);
                HttpContext.Current.Response.Cookies.Add(ck);

                outputText = "storing master page setting in cookie.";
            }
            else
            {
                // get the master page from cookie
                outputText = "retrieving master page setting from cookie.";
            }
                        
            HttpContext.Current.Response.Write(outputText + "<br/>");
        }

        private string GetCorrectMasterPage()
        {
            // logic goes here to get the correct master page
            return "/_catalogs/masterpage/BlackBand.master";

        }
HTTP Module and Cookies in Sharepoint 2007
===

I have some proof concept code for a HTTP module. The code checks to see if a cookie exists, if so it retrieves a value, if the cookie does not exist it creates it and sets the value.

Once this is done I write to the screen to see what action has been taken (all nice and simple). So on the first request the cookie is created; subsequent requests retrieve the value from the cookie. 

When I test this in a normal asp.net web site everything works correctly – yay! However as soon as I transfer it to SharePoint something weird happens, the cookie is never saved - that is the code always branches into creating the cookie and never takes the branch to retrieve the value - regardless of page refreshes or secondary requests.

Heres the code...

    public class SwithcMasterPage : IHttpModule
    {       

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        public void Init(HttpApplication context)
        {
            // register handler
            context.PreRequestHandlerExecute += new EventHandler(PreRequestHandlerExecute);
        }

        void PreRequestHandlerExecute(object sender, EventArgs e)
        {
            string outputText = string.Empty;

            HttpCookie cookie = null;
            string cookieName = "MPSetting";

            cookie = HttpContext.Current.Request.Cookies[cookieName];
            if (cookie == null)
            {
                // cookie doesn't exist, create
                HttpCookie ck = new HttpCookie(cookieName);
                ck.Value = GetCorrectMasterPage();
                ck.Expires = DateTime.Now.AddMinutes(5);
                HttpContext.Current.Response.Cookies.Add(ck);

                outputText = "storing master page setting in cookie.";
            }
            else
            {
                // get the master page from cookie
                outputText = "retrieving master page setting from cookie.";
            }
                        
            HttpContext.Current.Response.Write(outputText + "<br/>");
        }

        private string GetCorrectMasterPage()
        {
            // logic goes here to get the correct master page
            return "/_catalogs/masterpage/BlackBand.master";

        }