Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set cookie from SignalR hub on the server

Is there anyway I can set a cookie from inside a SignalR hub, specifically the OnConnected method. I want to send a cookie with a session id.

I tried this but it didn't seem to work, it also looks awkward because I'm not sure why I need to provide a key value pair of a string and a cookie.

public override Task OnConnected()
{
    var guid = new Guid();
    Context.RequestCookies.Add("SessionID", new Microsoft.AspNet.SignalR.Cookie("SessionID", guid.ToString()));
    return null;
}
like image 211
Willem D'Haeseleer Avatar asked Feb 27 '13 13:02

Willem D'Haeseleer


2 Answers

This is an old question, but in case anyone stumbles on it, there's a way to add cookies from a hub in signalr 2.0+. The HttpContextBase can be accessed through hub request context, so you can do something like this:

var newCookie = new HttpCookie("cookieName", "cookieValue");
Context.Request.GetHttpContext().Response.Cookies.Add(newCookie);
like image 98
tattarrattat Avatar answered Nov 13 '22 20:11

tattarrattat


I eventually decided to set the cookie from the serving MVC controller, i did not find anyway to set this from in a SignalR call.

like image 24
Willem D'Haeseleer Avatar answered Nov 13 '22 20:11

Willem D'Haeseleer