Null Cookies Collection

Aug 4, 2011 at 7:46 PM

I've been working to get the sample project up and running (and create my own) and am having troubles receiving cookies on the server.  I see in the included sample project SuperWebSocketWeb, the default.aspx page appends a cookie named 'name' and then on server connect (within Global.asax) you try to read session.Cookies["name"].  In this context, the property session.Cookies is always empty.

I did however create a sample forms project where I am able to specify the cookie named 'name' like :

var cookies = new List<KeyValuePair<string, string>>();
cookies.Add(new KeyValuePair<string, string>("name", this.tbName.Text));
this._socketClient = new WebSocket(this.tbServerAddress.Text, "basic", cookies);

When registering from desktop in this manner, the cookie is available within the NewSessionConnected event.  I'm pretty new to all this, so it's entirely possible I'm missing something simple - but any guidance would be greatly appreciated - I need my cookies! :)


Matt Klinker

Aug 5, 2011 at 7:30 PM

I went ahead and dug a bit deeper and found that I was actually sending duplicate cookies, and the code within WebSocketSession.cs was not handling this case.  I confirmed the fix by adding a check to the SetCookies method as follows:

if (!cookies.ContainsKey(key))
    cookies.Add(key, Uri.UnescapeDataString(value));

And also can confirm that code as is worked just fine once I cleared out my local cookies.  I realize that it's not good to have multiple cookie names (in this case it was actually Google Analytics that were duplicated); but from what I gather from this discussion on SO: it's definitely a real possibility and I think the session should handle this a bit more gracefully.

Thanks for the great project, appreciate all the hard work that must've gone into this.

Matt Klinker

Aug 6, 2011 at 1:23 AM

OK, I'll enhance it!

Sep 11, 2011 at 12:08 PM

I have fixed this issue in this change on my way:

Sep 13, 2011 at 2:46 AM

I cleared my cookies but I still have the same problem. When I set a cookie, its not being passed to the web socket connection. It works perfectly in the sample project but not in mine. I am setting the cookie through a web service call. I will check and see if that makes a difference to setting the cookie using a web forms postback.

Sep 13, 2011 at 2:59 AM

I figured out my own problem. Sorry about that. I had hard coded the address to localhost, rather than They both respond fine but I set my cookies ont he I did not set my cookies on localhost. Once I changed the WS connect string to I can see that I have cookie access now. Very good!