C#C
C#7mo ago
Majestic

Blazor Server - How to get client IpAddress and UA

In a Blazor Server application, what is the correct method to get the IpAddress and User Agent of a connecting client/user?

I have tried injecting IHttpContextAccessor and calling things like HttpContext.Connection.RemoteIpAddress to get the address in both a custom UserCircuitService and via middleware added into the program.cs.

The issue I have is that this all works on my local machine, however, on my production cloud server HttpContext is always null. And according to the Microsoft documentation (https://learn.microsoft.com/en-us/aspnet/core/blazor/components/httpcontext?view=aspnetcore-9.0) it should not be used, yet I can't see any other way to get the details of the connecting client.

The same code used to work on an older server as we originally had the code since .NET 6, but now having updated to .NET 9 on a new Windows 2025 build, the HttpContext is always null.

Surely there is an approved method of retrieving basic details of a connecting client?
Learn about IHttpContextAccessor and HttpContext in ASP.NET Core Blazor apps.
Was this page helpful?