C# 为什么在Windows窗体中心上下文中Context.User.Identity.Name为空?
我需要将C# 为什么在Windows窗体中心上下文中Context.User.Identity.Name为空?,c#,.net,winforms,signalr,C#,.net,Winforms,Signalr,我需要将User.Identity.Name传递给Windows窗体客户端 方法 public override Task OnConnected() { string userName = Context.User.Identity.Name; string connectionId = Context.ConnectionId; var user = Users.GetOrAdd(userName, _ => new User { Name
User.Identity.Name
传递给Windows窗体客户端
方法
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();
}
public override任务OnConnected(){
字符串userName=Context.User.Identity.Name;
字符串connectionId=Context.connectionId;
var user=Users.GetOrAdd(用户名,\=>newuser{
Name=用户名,
connectionId=newhashset()
});
锁(user.connectionId){
user.connectionId.Add(connectionId);
if(user.connectionId.Count==1){
Clients.Others.userConnected(用户名);
}
}
返回base.OnConnected();
}
但是
Context.User.Identity.Name
为空?为什么?如何解决此问题?连接到集线器时,您似乎正在尝试获取用户名。我通过从我的客户端传递用户名解决了类似的问题。听起来您正在使用signar.NET客户端。试试这个
客户端
Connection = new HubConnection("http://.../", new Dictionary<string, string>
{
{ "UserName", WindowsIdentity.GetCurrent().Name }
});
也许你遇到了这里描述的相同问题:基本上,你必须在设置身份验证后调用app.mapsigner。我在我的中心遇到了问题,这是一个巨大的错误,但我通过你的方法传递用户名,成功地使我的服务器端代码正常工作。谢谢
public override Task OnConnected()
{
string userName = Context.QueryString["UserName"]
}