【问题标题】:Connecting to a SignalR hub from console using ClaimsPrincial使用 ClaimsPrincial 从控制台连接到 SignalR 集线器
【发布时间】:2021-03-23 09:42:09
【问题描述】:

我有一个非常简单的 SignalR 集线器,它已在我的服务器上启动并运行。使用来自 Micorsoft 的示例,我创建了一个内存存储来将用户从 ClaimsPrincipal 映射到内存字典。代码如下:

 public class NotificationHub : Hub
{
    private readonly static ConnectionMapping<string> _connections = new ConnectionMapping<string>();
    
    public void SendNotification(string who, NotificationData message)
    {
        string name = Context.User.Identity.Name;
        foreach(var connectionId in _connections.GetConnections(who))
        {
            Clients.Client(connectionId).SendAsync("Notification", message);
        }
    }

    public override Task OnConnectedAsync()
    {
        
        string name = Context.UserIdentifier;
        _connections.Add(name, Context.ConnectionId);
        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(System.Exception exception)
    {
        string name = Context.User.Identity.Name;
        _connections.Remove(name, Context.ConnectionId);
        return base.OnDisconnectedAsync(exception);
    }
}

我想使用控制台应用程序对此进行测试,但我不知道如何在 .net 核心控制台应用程序中创建 ClaimsPrincipal。这是我的控制台应用程序代码:

class Program
{
    static async Task Main(string[] args)
    {

        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
        Console.WriteLine("Press a key to start listening");
        Console.ReadKey();

        Console.WriteLine("Client Listening!");
        var connection = new HubConnectionBuilder()
           .WithUrl("https://localhost:44375/Notify")
           .WithAutomaticReconnect(new[] { TimeSpan.Zero, TimeSpan.Zero, TimeSpan.FromSeconds(10) })
          //.ConfigureLogging(logging =>
          //{
          //    logging.AddConsole();
          //    logging.SetMinimumLevel(LogLevel.Debug);
          //})
           .Build();




        connection.On<NotificationData>("Notification", (notificationData) =>
            Console.WriteLine($"Somebody connected: {notificationData.ClientId}"));

        connection.StartAsync().GetAwaiter().GetResult();
      
        while(Console.ReadKey().Key == ConsoleKey.S)
        {
            var msg = new NotificationData { ClientId = Guid.NewGuid().ToString(), Notifications = null };
            await connection.SendAsync("SendNotification", msg);
            
        }

        
        Console.WriteLine("Listening. Press a key to quit");
        Console.ReadKey();

        connection.Reconnecting += error =>
        {
            Console.WriteLine("Reconnecting...");
            return Task.CompletedTask;
        };
    }
}

我尝试使用 WindowsPrincipal,但未能成功发送名称。显然 SignalR 需要有一个声明主体。如何从控制台应用程序发送 claimprincial?

【问题讨论】:

    标签: c# signalr signalr.client


    【解决方案1】:

    我能够创建一个 QueryBuilder 对象以从已发布的 JSON 文档中截取 userId,并创建一个附加了 QueryBuilder 项目的 URI 字符串。然后在 Hub 的 OnConnectedAsync 方法上,我使用 Context.GetHttpContext 方法从查询字符串中拉出用户并将其添加到组中。然后我将它映射到内存字典中,以将 userId 映射到 Context.ConnectionId。

    【讨论】:

      猜你喜欢
      • 2016-04-04
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 2019-10-12
      • 1970-01-01
      • 2021-03-23
      • 2017-03-11
      • 1970-01-01
      相关资源
      最近更新 更多