azure-signalr
azure-signalr copied to clipboard
Can not get request headers in Hub(Context.GetHttpContext) using Azure SignalR
I am using Azure SignalR, and i can not get request headers in my hub class. I was trying to do that using Context.GetHttpContext() method. but it was not successful. Can anyone help me? Thank you.
It should be in Context.GetHttpContext().Request.Headers.

Which header do you want? Are you using AspNetCore SignalR or AspNet SignalR?
I am seeing this too. I set custom headers when creating my hub connection in a Blazor WASM app.
hubConnection = new HubConnectionBuilder()
.WithUrl(Nav.ToAbsoluteUri("/hub"), (conn) =>
{
conn.Headers.Add("headerName", "headerValue");
})
.Build();
But this custom header is not present in Context.GetHttpContext().Request.Headers.
@MaxxDelusional I did a quick try and the headers are included in Context.GetHttpContext().Request.Headers.
C# client (tried both Microsoft.AspNetCore.SignalR.Client 3.1.25 and 6.0.5)
And server hub can get the custom header through Context.GetHttpContext().Request.Headers

Could you provide me with a minimum project that can repro the issue you described?
I am seeing this too.
I see this too ;), also in a blazor wasm project. The headers are included within the negotiate call. But I not see the headers within the OnConnectedAsync() overload.
I can not observe the headers within the second request made by the Microsoft.AspNetCore.SignalR.Client.Core.HubConnection.
@vicancy where are you reading the headers in your hub? Are they available in OnConnectedAsync?
I believe the problem might be caused by web sockets. I am not sure that web sockets support custom headers.
@MaxxDelusional It's indeed in OnConnctedAsync, but I am using a normal SignalR project instead of a blazor WASM project though. A minimum project that can repro the issue is very much appreciated.
I am also using the SignalR in Blazor WASM, add custom header on client, in my expection, there should have jimmy in the header, unfortunate there have any custom header, do you have any idea to resolve?
my code

network request

You can't set headers on WebSocket requests in the browser. This is a limitation of the browser and not something SignalR can fix. See https://github.com/whatwg/websockets/issues/16 if you're passionate about browsers supporting this.
You can't set headers on WebSocket requests in the browser. This is a limitation of the browser and not something SignalR can fix. See whatwg/websockets#16 if you're passionate about browsers supporting this.
Thanks for your information, I think I should set the data on sending.