Why Context.User.Identity.Name is null in Windows

2019-07-17 19:52发布

I need to pass User.Identity.Name to Windows Form client.

Method

public override Task OnConnected() {

    string userName = Context.User.Identity.Name;
    string connectionId = Context.ConnectionId;

    var user = Users.GetOrAdd(userName, _ => new User {
        Name = userName,
        ConnectionIds = new HashSet<string>()
    });

    lock (user.ConnectionIds) {
        user.ConnectionIds.Add(connectionId);
        if (user.ConnectionIds.Count == 1) {
            Clients.Others.userConnected(userName);
        }
    }
    return base.OnConnected();
}

But Context.User.Identity.Name is null? Why? How to solve it?

1条回答
再贱就再见
2楼-- · 2019-07-17 20:15

It looks like you are trying to get the username when connecting to the hub. I solved a similar issue by passing the username from my client. It also sounds like you are making use of the SignalR .NET client. Give this a try

Client

Connection = new HubConnection("http://.../", new Dictionary<string, string>
{
    { "UserName", WindowsIdentity.GetCurrent().Name }
});

Hub

public override Task OnConnected()
{
    string userName = Context.QueryString["UserName"]
}
查看更多
登录 后发表回答