'SignalR Client How to Set user when start connection?
Server side:
public override Task OnConnected()
{
var connectionId = Context.ConnectionId;
var user = Context.User.Identity.Name; // Context.User is NULL
return base.OnConnected();
}
Client side (in Console project):
IHubProxy _hub;
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.Start().Wait();
When the client connect to the server, I want to know the map between userName and connectionId, But Context.User
is NULL. How do I set this value in the client side?
Solution 1:[1]
Pass your username using query string.
Client
First set query string
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.qs = { 'username' : 'anik' };
connection.Start().Wait();
Server
public override Task OnConnected()
{
var username= Context.QueryString['username'];
return base.OnConnected();
}
Solution 2:[2]
try this with queryString in asp.netcore 2.1:
Client (javascript) set query string after url like follow:
var connection = new signalR.HubConnectionBuilder().withUrl("http://localhost:10499/chathub?username=xxxx").build();
connection.start().then(function ()
{
// do some thing here ...
}).catch(function (err)
{
console.error(err.toString());
});
.
.
.
Server
public override Task OnConnectedAsync()
{
var username = Context.GetHttpContext().Request.Query["username"];
// username = xxxx
return base.OnConnectedAsync();
}
Solution 3:[3]
Client
var connection = new HubConnection(<YOUR_URL>);
connection.Headers.Add("username", "maria");
var myHub = connection.CreateHubProxy("MyHub");
Server
string username = Context.Headers.Get("username");
Console.WriteLine("New client connection - " + username);
Solution 4:[4]
If your using basic authentication create a new System.Net.NetworkCredential
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
NetworkCredential myCredentials = new NetworkCredential("","","");
myCredentials.Domain = "domain";
myCredentials.UserName = "username";
myCredentials.Password = "passwd";
connection.Credentials = myCredentials;
_hub = connection.CreateHubProxy("TestHub");
connection.Start().Wait();
See: https://docs.microsoft.com/en-us/dotnet/api/system.net.networkcredential.username?view=net-6.0
Solution 5:[5]
try this
Client (C#)
//Enter query string
var querystringData = new Dictionary<string, string>();
querystringData.Add("username", "naveed");
IHubProxy _hub;
string url = @"http://localhost:8080/";
var connection = new HubConnection(url);
_hub = connection.CreateHubProxy("TestHub");
connection.Start().Wait();
connection.Start().Wait();
Server
public override Task OnConnected()
{
var connectionId = Context.ConnectionId;
var username= Context.QueryString["username"]; //here you will receive naveed as username
return base.OnConnected();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Anik Islam Abhi |
Solution 2 | AminRostami |
Solution 3 | Jéssica Machado |
Solution 4 | Ryan Dooley |
Solution 5 | Naveed Ahmed |