How to read headers data in hub SignalR ASP.NET Core 2.1

折月煮酒 提交于 2021-02-20 09:47:00

问题


I'm trying to pass userId to hub on connection to signalR. This is how client sets up the connection:

         connection = new HubConnectionBuilder()
            .WithUrl("http://localhost:56587/hub", options =>
            {
                options.Headers["UserId"] = loginTextBox.Text;
            })
            .AddMessagePackProtocol()
            .Build();

How can I read this header in OnConnectedAsync() method in my hub?


回答1:


To get Header Value as string:

public override async Task OnConnectedAsync()
{
    var httpCtx = Context.GetHttpContext();
    var someHeaderValue = httpCtx.Request.Headers["UserId"].ToString();
}

Note - You may want to consider passing information in the query string however as not all transports support headers.



来源:https://stackoverflow.com/questions/51749088/how-to-read-headers-data-in-hub-signalr-asp-net-core-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!