Get IP Address in ASP.NET Core 3.x

access_time 2 months ago visibility207 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();
	<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:


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. 

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

Want to publish your article on Kontext?

Learn more

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.

Learn more arrow_forward

More from Kontext

C# 9.0 New Features

local_offer C# local_offer .NET

visibility 121
thumb_up 0
access_time 2 months ago

.NET 5.0 release candidate 1 (rc.1) was published on 2020-09-14, which marks another big step towards the official .NET 5.0 release. As part of 5.0, C# 9.0 will be released with a bunch of new features. This article summarizes some of the new features with examples. Download .NET 5.0 SDK from this ...

local_offer C#

visibility 9
thumb_up 0
access_time 2 months ago

C# regular expressions can be used to match and replace certain text patterns from a string variable. The following regular expression can be used to remove all heading tags incl. h1 to h9 from HTML text string. <[hH][1-9][^>]*>[^<]*</[hH][1-9]\s*> var html = "Your HTML ...

local_offer core local_offer C#

visibility 258
thumb_up 0
access_time 2 months ago

Microsoft 365 and Office 365 supports using licensed user account to send emails in client applications. SMTP protocol can be used to send emails in ASP.NET Core applications. This article shows how to use SMTP AUTH to send emails in ASP.NET Core 3.1 applications. warning  This guide is ...

About column

Articles about ASP.NET Core 1.x, 2.x and 3.x.

rss_feed Subscribe RSS