Search code examples
.net-coresignalr

How to add SignalR request Headers on .net client side


I have server side Hub with the following code:

        private string GetUserPhoneFromContext()
        {
            var httpCtx = Context.GetHttpContext();
            return httpCtx.Request.Headers["userPhone"].ToString();
        }

Mention above code I can't change it works on prod with flutter clients. I am developing blazor wasm client side and on client side I want to add headers as follow:

 hubConnection = new HubConnectionBuilder()
                .WithUrl($"{Uri}?userPhone={Phone}", options =>
                {
                    options.Headers.Add("userPhone", Phone);                    
                })
                .WithAutomaticReconnect()
                .Build();

But nor via options.Header.Add() neither via adding query string parameter I can't read header on the server side via GetUserPhoneFromContext method, I always get empty string instead of added on client userPhone header. Not clearly understand why it works with flutter clients but does not work with .net blazor wasm


Solution

  • I find out the root. The Blazor WASM SignalR implemetation is a wrapper around JS implementation that does not support Headers. If you need additional data use QueryString

    see https://github.com/dotnet/aspnetcore/issues/18694 for more