Get IP Address in ASP.NET Core 5

Raymond Raymond event 2021-01-16 visibility 41,189 comment 2
more_vert

This page summarize information about how to retrieve client and server IP address in ASP.NET Core 5 (.NET 5) web applications. This page is an updated version of Get IP Address in ASP.NET Core 3.x.

Get client user IP address

Client IP address can be retrieved via HttpContext.Connection object. This properties exist in both Razor page model and ASP.NET MVC controller. Property RemoteIpAddress is the client IP address. The returned object (System.Net.IpAddress) can be used to check whether it is IPV4 or IPV6 address.

public string ClientIPAddr { get; private set; }

public async Task<IActionResult> OnGetAsync()
{
	// Retrieve client IP address through HttpContext.Connection
	ClientIPAddr = HttpContext.Connection.RemoteIpAddress?.ToString();
	return Page();
}

Get server IP address

Server or local IP addresses can be retrieved through HttpContext connection feature (IHttpConnectionFeature).

public string LocalIPAddr { get; private set; }

public async Task<IActionResult> OnGetAsync()
{
	// Retreive server/local IP address
	var feature = HttpContext.Features.Get<IHttpConnectionFeature>();
	LocalIPAddr = feature?.LocalIpAddress?.ToString();

	return Page();
}

Alternatively, server address can be retrieved through DNS.

For example, the following code runs in Razor page views directly:

@{
	var hostName = System.Net.Dns.GetHostName();
	@hostName
	<br />
	var ips = await System.Net.Dns.GetHostAddressesAsync(hostName);
	foreach (var _ in ips)
	{
		@_
		<br />
	}
}

The output looks like the following when run the code in a local development machine:

***
172.28.0.1
192.168.119.1
192.168.64.1
192.168.1.103
::1

Multiple IP addresses are shown for all the available networks. 

The above example can also work in Console or Desktop application which doesn't have a HttpContext in the process.

Get client IP address in authorization handlers

In ASP.NET 5, HttpContext can be accessed via AuthorizationHandlerContext.Resource. And then client IP address can be retrieved using the first approach mentioned above.

protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, EditCommentByIdRequirement requirement)
{
            var httpContext = context.Resource as HttpContext;
}

Access HttpContext in ASP.NET Core

Refer to documentation Access HttpContext in ASP.NET Core | Microsoft Docs to understand the details of accessing HttpContext in ASP.NET Core applications.

For requests forwarded by proxy servers

If the HTTP request is first processed by a reverse proxy server, the only approach to get the client IP address in server side is to look into the forwarded headers if there are any. For example, Retrieve Client IP Address in Azure Container Apps with HTTP Ingress.

More from Kontext
comment Comments
U Udaysinh Vaghela

Udaysinh access_time 4 years ago link more_vert

Very useful article . Thanks. but

How can get client System name and client local machine user name?

Raymond Raymond

Raymond access_time 4 years ago link more_vert

For user OS system type, you can use User-Agent in request headers:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38

For example the above value means Windows 10 x64 edition. 

As there are many browsers and many system types and versions, it is not easy to do a holistic parsing. What you can do is to find out the most useful ones for you based on each browser User-Agent definitions. 

For client local user machine name, it is not sent to the server in the request unless you are using Windows Authentication. Even for this authentication type, it doesn't necessarily represent the actual user name as user can use Run As to impersonate users.

Configure Windows Authentication in ASP.NET Core | Microsoft Docs

And then you can use the following approach in Razor Pages or Controllers to get user name:

string userName = HttpContext.User.Identity.Name;


Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts