การเชื่อมต่อกับฮับ SignalR จากคอนโซลโดยใช้ ClaimsPrincial

ฉันมีฮับ 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);
    }
}

ฉันต้องการทดสอบสิ่งนี้ด้วยแอปพลิเคชันคอนโซล แต่ฉันไม่สามารถหาวิธีสร้าง ClaimsPrincipal ในแอปคอนโซลหลัก .net ได้ นี่คือรหัสแอปคอนโซลของฉัน:

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 จำเป็นต้องมีหลักการเรียกร้อง คุณจะส่งการเรียกร้องหลักจากแอปพลิเคชันคอนโซลได้อย่างไร


person john    schedule 12.12.2020    source แหล่งที่มา


คำตอบ (1)


ฉันสามารถสร้างออบเจ็กต์ QueryBuilder เพื่อสกัดกั้น userId จากเอกสาร JSON ที่โพสต์ และสร้างสตริง URI โดยมีรายการ QueryBuilder ต่อท้าย จากนั้นในเมธอด OnConnectedAsync ของ Hub ฉันใช้วิธีการ Context.GetHttpContext เพื่อดึงผู้ใช้ออกจากสตริงการสืบค้นและเพิ่มลงในกลุ่ม จากนั้นฉันแมปมันกับพจนานุกรมในหน่วยความจำเพื่อแมป userId กับ Context.ConnectionId

person john    schedule 17.12.2020