22

I implement the Connect() method on IConnected interface to add new connections to the correct groups. This works well except for one thing: in order to add the user to the correct group, I need to send a value to be read in this method. I tried adding property to the client hub:

var uIHub = $.connection.uIHub;
uIHub.SessionType = "Edit";

But it's not accessible from the Connect method:

if (string.IsNullOrEmpty(Caller.SessionType) || Caller.SessionType == "Edit") {
     sessionId = WAFContext.EditSession.SessionId.ToString();                
} else {
     sessionId = WAFContext.ViewSession.SessionId.ToString();
}
Groups.Add(Context.ConnectionId, sessionId);

Caller.SessionType is always null.

Any suggestions on how to solve this?

Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
Vidar Langberget
  • 457
  • 1
  • 3
  • 14

1 Answers1

44

I solved this by adding my information to the querystring, which is available on the IConnected.Connect() method.

On the .NET client you pass the querystring into your HubConnection:

var connection = new HubConnection("http://localhost:8080/", "myInfo=12345");

On the JS client, you set the qs property before starting the connection:

$.connection.hub.qs = "myInfo=12345";

You can then access this information on the server in the Connect() method:

var myInfo = Context.QueryString["myInfo"];
Groups.Add(Context.ConnectionId, myInfo);
Pierre Arnaud
  • 10,212
  • 11
  • 77
  • 108
Alexander Köplinger
  • 2,497
  • 17
  • 15
  • Do you know how to do the same with the javascript client? I've tried the following, but it's not working: var connection = $.connection("http://localhost:61806/" , "sessionType=visit"); var uIHub = connection.uIHub; – Vidar Langberget Sep 18 '12 at 10:35
  • 1
    This should work: `$.connection.hub.qs = "myInfo=12345"; $.connection.hub.start();` – Alexander Köplinger Sep 18 '12 at 11:16
  • @akoeplinger is it safe to pass userid and username parameters in this way ? Can cause any security problem? – Freshblood Dec 09 '13 at 01:01
  • @freshblood It's no different than the security concerns that you'd have if you were passing it through a form on the page; if you're not doing it behind https, then you have a huge security hole. – casperOne Dec 12 '13 at 12:42
  • I used your suggested approach to pass data, when I'm using IE it's not passed with Unicode and this is what I get on the server: "������". Any idea how to solve it? – Akbari Mar 10 '16 at 04:04
  • 2
    Does Reconnect also receive the data from QueryString? – Sellorio Jul 30 '17 at 11:39