Why Context.User.Identity.Name is null in Windows Form Hub Context?

本秂侑毒 提交于 2019-12-10 10:49:07

问题


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:


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"]
}


来源:https://stackoverflow.com/questions/22238951/why-context-user-identity-name-is-null-in-windows-form-hub-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!