azure-signalr icon indicating copy to clipboard operation
azure-signalr copied to clipboard

Can not get request headers in Hub(Context.GetHttpContext) using Azure SignalR

Open dreamofmaks opened this issue 4 years ago • 9 comments

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.

dreamofmaks avatar Nov 11 '21 12:11 dreamofmaks

It should be in Context.GetHttpContext().Request.Headers. image

Which header do you want? Are you using AspNetCore SignalR or AspNet SignalR?

vicancy avatar Nov 15 '21 05:11 vicancy

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 avatar May 21 '22 02:05 MaxxDelusional

@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) image And server hub can get the custom header through Context.GetHttpContext().Request.Headers image

Could you provide me with a minimum project that can repro the issue you described?

vicancy avatar May 24 '22 02:05 vicancy

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.

cssack avatar May 24 '22 08:05 cssack

@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 avatar May 24 '22 12:05 MaxxDelusional

@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.

vicancy avatar May 25 '22 09:05 vicancy

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 image

network request image

jiaming0708 avatar Sep 06 '22 08:09 jiaming0708

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.

BrennanConroy avatar Sep 06 '22 16:09 BrennanConroy

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.

jiaming0708 avatar Sep 07 '22 06:09 jiaming0708