The best that I was able to come up with was to create a method which invokes a web form to get/set session data. I understand that this isn't very efficient however I don't see any other way to do this.
Edit: In order for this to work you need to enable Cookieless sessions in the Web.Config file. This could be a big security risk for most sites so remember to implement logic to prevent session hijacking! I am currently using IP verification along with browser fingerprint checking.
I created a class that I use in my WebSocket handler (WSHandler.ashx):
public class SessionData
{
AspNetWebSocketContext _WebSocketContext = null;
public SessionData(AspNetWebSocketContext WebSocketContext)
{
_WebSocketContext = WebSocketContext;
}
public string GetSessionValue(string name)
{
string value = "";
using (WebClient wc = new WebClient())
{
string Url = _WebSocketContext.Origin + _WebSocketContext.RawUrl.Replace("WSHandler.ashx", "UpdateSession.aspx");
Url += "?a=1";
Url += "&n=" + name;
value = wc.DownloadString(Url);
}
return value;
}
public void PutSessionValue(string name, string value)
{
using (WebClient wc = new WebClient())
{
string Url = _WebSocketContext.Origin + _WebSocketContext.RawUrl.Replace("WSHandler.ashx", "UpdateSession.aspx");
Url += "?a=2";
Url += "&n=" + name;
Url += "&v=" + value;
wc.DownloadString(Url);
}
}
}
Below is the form for the web form UpdateSession.aspx:
public partial class UpdateSession : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string action = Page.Request.QueryString["a"];
string name = Page.Request.QueryString["n"];
string value = Page.Request.QueryString["v"];
Page.Response.Clear();
if (string.IsNullOrEmpty(name) == true)
{
return;
}
switch (action)
{
case "1":
if (Session[name] != null)
{
Page.Response.Write(Session[name].ToString());
}
break;
case "2":
Session[name] = value;
break;
}
Page.Response.End();
}
}