Get IP Address in ASP.NET Core 3.x

access_time 5 months ago visibility3012 comment 0

This page summarize information about how to retrieve client and server IP address in ASP.NET core applications. 

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. 

info Last modified by Raymond 2 months ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn or Twitter.

Want to publish your article on Kontext?

Learn more

More from Kontext

visibility 65
thumb_up 0
access_time 2 years ago

This code snippet shows how to convert string to date.

visibility 154
thumb_up 0
access_time 5 months ago

Language-Integrated Query (LINQ) is a set of technologies based on the integration of query capabilities directly into the C# or VB language in .NET. It allows intuitive query against SQL databases, XML, object list, etc.  This article shows how to return a top N records randomly.  The ...

Introduction to C# Interactive
visibility 464
thumb_up 0
access_time 5 months ago

Python, R and many other scripting languages generally support interactive programming features in their IDEs. When C# was created initially, all C# written programs need to be complied into MSIL first before it can run in .NET runtime environments (unless the code is dynamically complied).  ...