2014-03-07 60 views
2

我需要将User.Identity.Name传递给Windows窗体客户端。为什么Context.User.Identity.Name在Windows Form Hub上下文中为null?

方法

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(); 
} 

Context.User.Identity.Name为空?为什么?如何解决它?

+0

也许你正在运行到这里所描述的同样的问题:http://stackoverflow.com/questions/22002092/context-user-identity-name-is-null-with-signalr-2-0-2-how-to-fix-it基本上,你必须在设置你的认证之后调用app.MapSignalR *。 – halter73

+0

http://stackoverflow.com/questions/35794541/how-assign-a-connectionid-to-username-which-is-send-from-client-to-server-in-sig – Khalid

回答

5

看起来您正在尝试在连接到集线器时获取用户名。我通过从我的客户端传递用户名解决了类似的问题。这听起来像你正在使用SignalR .NET客户端。试试这个

客户

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

枢纽

public override Task OnConnected() 
{ 
    string userName = Context.QueryString["UserName"] 
} 
+1

我遇到了麻烦[ Context.User.Name是空的](http://stackoverflow.com/questions/34709307/signalr-context-user-name-returns-empty)在我的枢纽,这是一个巨大的诡计,但我设法让我的服务器端通过您的方法传递用户名的代码。谢谢! – KidCode

相关问题