我需要將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
爲空?爲什麼?如何解決它?
也許你正在運行到這裏所描述的同樣的問題:http://stackoverflow.com/questions/22002092/context-user-identity-name-is-null-with-signalr-2-0-2-how-to-fix-it基本上,你必須在設置你的認證之後調用app.MapSignalR *。 – halter73
http://stackoverflow.com/questions/35794541/how-assign-a-connectionid-to-username-which-is-send-from-client-to-server-in-sig – Khalid