Reputation: 1147
As we know the id of new connection can be setted using custom IUserIdProvider
.
public string GetUserId(IRequest request)
{
var context = request.GetHttpContext();
...
}
this method must return new connection Id. My web application architecture is simple:
When uses connected, application generates guid (string) can be acessed from javascript variable. Then use generated guid in internals. So the question is how can i pass additional variable to signalR request to use it in GetUserId
method.
Thanks!
Upvotes: 2
Views: 8695
Reputation: 15234
First, let's clear up some terminology. When you are setting a custom IUserIdProvider you aren't actually changing the client's connection id, but instead the client's user id.
The client's connection id is always a GUID that is generated randomly by SignalR when each connection is started. This cannot be changed. To send a message to a client from a hub given its connection id, you would write: Clients.Client(connectionId).myMethod(...)
.
The client's user id is the user's IPrincipal.Identity.Name
by default, but this can be changed given a custom IUserIdProvider
. Unlike a connection id, a user id can be shared by multiple connections. A common scenario where this can happen is when a single user has an app using SignalR open in multiple browser tabs. To send a message to a user from a hub given a user id, you would write: Clients.User(userId).myMethod(...)
.
To answer your question, you can set a query string parameter that will be sent with every SignalR request which you can read in your GetUserId
method. Be aware, that an attacker could easily set their own query string to whatever they want. This could make it possible to impersonate a user if you aren't careful about how you design your app.
Upvotes: 4