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?

Claudio P
  • 2,133
  • 3
  • 25
  • 45
wtf512
  • 4,487
  • 9
  • 33
  • 55
  • What kind of authentication do you use? If you use ASP.NET built in authentication it should just work – Anders Apr 08 '15 at 08:26
  • I didn't use any kind of authentication ... I just build a new empty project and download signalr from NuGet – wtf512 Apr 08 '15 at 08:32
  • Well you need to have Authentication enabled and then Context.User will just work – Anders Apr 08 '15 at 08:39
  • 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) – Anders Apr 08 '15 at 08:43
  • In case you are using the **Azure SignalR Service** see [How to use SignalR to send data to a specific user?](https://stackoverflow.com/a/58996650/7910454) – leonheess Nov 20 '20 at 11:51

5 Answers5

21

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();
    }
AminRostami
  • 2,585
  • 3
  • 29
  • 45
  • I have [a similar question](https://stackoverflow.com/q/58630559/7910454) and would appreciate any help a lot – leonheess Nov 22 '19 at 11:29
  • 1
    This is the most acceptable one according to me. I tried all the others but to no avail. This required a double + – Venugopal M Sep 29 '20 at 16:44
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();
}
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
  • I have [a similar question](https://stackoverflow.com/q/58630559/7910454) and would appreciate any help a lot – leonheess Nov 22 '19 at 11:28
  • 6
    '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?) – HOÀNG LONG Feb 29 '20 at 13:00
  • @HOÀNGLONG old ASP.NET SignalR or new ASP.NET Core SignalR? – TimTIM Wong Sep 14 '21 at 22:26
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);
  • 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 where – weegee Jun 21 '19 at 18:22
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

Ryan Dooley
  • 224
  • 1
  • 3
  • 16
-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();
}
Naveed Ahmed
  • 463
  • 4
  • 11