Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dreamofmaks opened this issue Nov 11, 2021 · 9 comments

Comments

@dreamofmaks
Copy link

dreamofmaks commented Nov 11, 2021

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.

@vicancy
Copy link
Member

vicancy commented Nov 15, 2021

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

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

@MaxxDelusional
Copy link

MaxxDelusional commented May 21, 2022

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.

@vicancy
Copy link
Member

vicancy commented May 24, 2022

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

@cssack
Copy link

cssack commented May 24, 2022

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.

@MaxxDelusional
Copy link

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

@vicancy
Copy link
Member

vicancy commented May 25, 2022

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

@jiaming0708
Copy link

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

@BrennanConroy
Copy link
Collaborator

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.

@jiaming0708
Copy link

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.

@vicancy vicancy closed this as completed Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants