Search code examples
c#.net-6.0httpcontextasp.net-core-signalrhttpconnection

AspNetCore SignalR, can't read headers and HttpContext


I'm developing server(console app) on latest .Net 6 platform, using AspNetCore.signalR and in OnConnectedAsync method I want to read headers present in HttpContext, but I can't get HttpContext, I've tried:

var httpContext = Context.GetHttpContext();

But this throws compilation error. Like this method does not exist in this namespace anymore(namespace: Microsoft.AspNetCore.SignalR).

enter image description here

I also tried following approach:

var features = Context.Features.Get<HttpConnectionFeature>();

But this only gives info about Local/Remote IP Addresses which is useless when using proxy.

So I want to read all context headers, like User-Agent, Remote IP address, X-Forwarded-For and etc.

Any suggestions/solutions?


Solution

  •     public override Task OnConnectedAsync()
        {
            var httpCtx = Context.GetHttpContext();
            var headers = httpCtx.Request.Headers;
            return base.OnConnectedAsync();
        }
    

    enter image description here