SignalR Client How to Set user when start connection?
Asked Answered
P

5

18

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?

Puke answered 8/4, 2015 at 8:18 Comment(5)
What kind of authentication do you use? If you use ASP.NET built in authentication it should just workChunk
I didn't use any kind of authentication ... I just build a new empty project and download signalr from NuGetPuke
Well you need to have Authentication enabled and then Context.User will just workChunk
Since you use .NET client you also need to set the credentails on the client hubConnection.Credentials = CredentialCache.DefaultCredentials (This code applies to Windows authentication)Chunk
In case you are using the Azure SignalR Service see How to use SignalR to send data to a specific user?Introduction
W
14

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();
}
Wham answered 8/4, 2015 at 8:23 Comment(3)
I have a similar question and would appreciate any help a lotIntroduction
'HubConnection' does not contain a definition for 'qs' and no accessible extension method 'qs' accepting a first argument of type 'HubConnection' could be found (are you missing a using directive or an assembly reference?)Specialty
@HOÀNGLONG old ASP.NET SignalR or new ASP.NET Core SignalR?Nacreous
A
20

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();
    }
Arman answered 18/4, 2019 at 8:11 Comment(2)
I have a similar question and would appreciate any help a lotIntroduction
This is the most acceptable one according to me. I tried all the others but to no avail. This required a double +Procarp
W
14

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();
}
Wham answered 8/4, 2015 at 8:23 Comment(3)
I have a similar question and would appreciate any help a lotIntroduction
'HubConnection' does not contain a definition for 'qs' and no accessible extension method 'qs' accepting a first argument of type 'HubConnection' could be found (are you missing a using directive or an assembly reference?)Specialty
@HOÀNGLONG old ASP.NET SignalR or new ASP.NET Core SignalR?Nacreous
G
2

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);
Godinez answered 21/6, 2019 at 18:20 Comment(1)
While this may answer the question, it is required that you mention and explain the changes and why you changed them. This will help the OP to learn and know why the changes were made and whereCreak
H
0

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://learn.microsoft.com/en-us/dotnet/api/system.net.networkcredential.username?view=net-6.0

Humdrum answered 20/4, 2022 at 1:21 Comment(0)
B
-1

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();
}
Bothwell answered 28/2, 2018 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.