为什么在Windows Form Hub上下文中Context.User.Identity.Name为null? [英] Why Context.User.Identity.Name is null in Windows Form Hub Context?

查看:47
本文介绍了为什么在Windows Form Hub上下文中Context.User.Identity.Name为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 User.Identity.Name 传递给Windows窗体客户端.

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

方法

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 是否为null?为什么?该怎么解决?

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

推荐答案

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

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

客户

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

中心

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

这篇关于为什么在Windows Form Hub上下文中Context.User.Identity.Name为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆