Can you please let me know how to get client IP address in ASP.NET when using MVC 6.
Request.ServerVariables["REMOTE_ADDR"]
does not work.

- 98,240
- 88
- 296
- 433

- 23,151
- 18
- 48
- 71
-
Example: `httpContext.GetFeature
().RemoteIpAddress` – Kiran Feb 23 '15 at 18:01
26 Answers
The API has been updated. Not sure when it changed but according to Damien Edwards in late December, you can now do this:
var remoteIpAddress = request.HttpContext.Connection.RemoteIpAddress;

- 17,596
- 6
- 52
- 72
-
13The `RemoteIpAddress` is always `null` for me, when I publish he website on IIS and log this on a file. – A-Sharabiani Feb 16 '16 at 18:52
-
Currently, there is a bug on this API which makes it return `null`. https://github.com/aspnet/IISIntegration/issues/17 – h2nghia Mar 09 '16 at 00:28
-
3
-
3System.Net.IPAddress remoteIpAddress = this.Request.HttpContext.Connection.RemoteIpAddress; – Stefan Steiger Oct 13 '16 at 04:41
-
23
-
70This is returning "::1" for me which is the IPv6 format. How are others seeing 127.0.0.1? – Derek Greer Oct 28 '16 at 15:36
-
11Does anyone else get the local IP address of their IIS server returned? – dave317 Apr 13 '18 at 18:09
-
1@DerekGreer it seems that it does not resolve localhost into IP address. If you use 127.0.0.1 instead of localhost it should show you the IP address. – Kosta_M Feb 24 '19 at 06:13
-
4context.HttpContext.Connection.RemoteIpAddress.MapToIPv4(); this returens 0.0.0.1 – Bukunmi Apr 09 '19 at 10:51
-
1
-
37Just a heads up, it's returning a "::1" because you are running it locally and that is what is always returned when running locally. – Andrew Reese Jan 14 '20 at 20:14
-
1This is the correct property, but if you have any type of reverse proxy, load balancer, or content distribution involved you will also need to configure UseForwardedHeaders(), as per the answer from Johna. – Sly Gryphon Sep 15 '22 at 07:01
In project.json add a dependency to:
"Microsoft.AspNetCore.HttpOverrides": "2.2.0"
In Startup.cs
, in the Configure()
method add:
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto
});
And, of course:
using Microsoft.AspNetCore.HttpOverrides;
Then, I could get the ip by using:
Request.HttpContext.Connection.RemoteIpAddress
In my case, when debugging in VS I got always IpV6 localhost, but when deployed on an IIS I got always the remote IP.
Some useful links: How do I get client IP address in ASP.NET CORE? and RemoteIpAddress is always null
The ::1
is maybe because of:
Connections termination at IIS, which then forwards to Kestrel, the v.next web server, so connections to the web server are indeed from localhost. (https://stackoverflow.com/a/35442401/5326387)
Edit 12/2020: Thanks to SolidSnake: as of Dec 2020 the latest version is 2.2.0
Edit 06/2021: Thanks to Hakan Fıstık: In .NET 5 the namespace is Microsoft.AspNetCore.Builder

- 2,623
- 3
- 21
- 36
-
19This is the correct answer, that is also documented on the official documentation about reverse proxies: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?tabs=aspnetcore2x#why-use-a-reverse-proxy-server – Melvyn Feb 27 '18 at 20:38
-
35need to point out that the "app.UseForwardedHeaders..." needs to be added before the app.UseAuthentication(); line, in case you use indentity – netfed Apr 06 '18 at 01:09
-
3This worked perfectly and I have tested is on locally hosted IIS and on Azure. Works both places. – ThomasCle Apr 27 '18 at 11:30
-
3
-
3
-
3@HakanFıstık Nothing changed, the `UseForwardedHeaders()` extension method and the `ForwardedHeadersOptions` class was always in `Microsoft.AspNetCore.Builder` while `ForwardedHeaders` is still in `Microsoft.AspNetCore.HttpOverrides` – yoel halb Aug 02 '21 at 23:11
-
2
-
Connection.RemoteIpAddress is correct; if you are using a content distribution network or additional layers of reverse proxies, you will need additional configuration of UseForwardedHeaders() based on your setup -- see the links for the official documentation for configuring advanced scenarios. – Sly Gryphon Sep 15 '22 at 06:59
Some fallback logic can be added to handle the presence of a Load Balancer.
Also, through inspection, the X-Forwarded-For
header happens to be set anyway even without a Load Balancer (possibly because of additional Kestrel layer?):
public string GetRequestIP(bool tryUseXForwardHeader = true)
{
string ip = null;
// todo support new "Forwarded" header (2014) https://en.wikipedia.org/wiki/X-Forwarded-For
// X-Forwarded-For (csv list): Using the First entry in the list seems to work
// for 99% of cases however it has been suggested that a better (although tedious)
// approach might be to read each IP from right to left and use the first public IP.
// http://stackoverflow.com/a/43554000/538763
//
if (tryUseXForwardHeader)
ip = GetHeaderValueAs<string>("X-Forwarded-For").SplitCsv().FirstOrDefault();
// RemoteIpAddress is always null in DNX RC1 Update1 (bug).
if (ip.IsNullOrWhitespace() && _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress != null)
ip = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
if (ip.IsNullOrWhitespace())
ip = GetHeaderValueAs<string>("REMOTE_ADDR");
// _httpContextAccessor.HttpContext?.Request?.Host this is the local host.
if (ip.IsNullOrWhitespace())
throw new Exception("Unable to determine caller's IP.");
return ip;
}
public T GetHeaderValueAs<T>(string headerName)
{
StringValues values;
if (_httpContextAccessor.HttpContext?.Request?.Headers?.TryGetValue(headerName, out values) ?? false)
{
string rawValues = values.ToString(); // writes out as Csv when there are multiple.
if (!rawValues.IsNullOrWhitespace())
return (T)Convert.ChangeType(values.ToString(), typeof(T));
}
return default(T);
}
public static List<string> SplitCsv(this string csvList, bool nullOrWhitespaceInputReturnsNull = false)
{
if (string.IsNullOrWhiteSpace(csvList))
return nullOrWhitespaceInputReturnsNull ? null : new List<string>();
return csvList
.TrimEnd(',')
.Split(',')
.AsEnumerable<string>()
.Select(s => s.Trim())
.ToList();
}
public static bool IsNullOrWhitespace(this string s)
{
return String.IsNullOrWhiteSpace(s);
}
Assumes _httpContextAccessor
was provided through DI.
-
5THIS is the right answer. There's not a single way to retrieve the IP address, specially when your app is behind a Nginx, a load balancer or something like that. Thanks! – Feu Oct 02 '16 at 02:54
-
@crokusek...trying to adapt your solution, but VS forcing me to the class encapsulating this code static. Do you have this code in your web app project or in a class library in the solution? – dinotom Apr 05 '18 at 17:07
-
The first 2 methods should be in an instance that provides __httpContextAccessor (or adapt it). The second 2 string methods were pulled from a separate static extensions class. – crokusek Apr 05 '18 at 17:22
-
This is a good solution especially when your app uses Kestrel and is hosted with Nginx on Linux. – Timothy Macharia Aug 17 '19 at 14:58
-
5Terrible answer if badly configured. Someone could fake the IP just by injecting X-Forwarded-For header if someone finds the true server's IP. – Lucca Ferri Jan 11 '20 at 21:39
-
This solution works for me, I have a deployment behind a K8s Ingress, also works great if you debug through Ngrok or other port forwarding solution. Some important points: the IsNullOrWhiteSpace and SplitCsv methods should go in a static class as they are extension methods, the other can be used in the context of a controller as the IHttpContextAccessor is required (can be injected through DI as mentioned). – Carlos Aug 12 '20 at 18:31
-
You should probably not do this manually like that, use the built-in `UseForwardedHeaders` middleware instead — see [documentation](https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/proxy-load-balancer). – Arad Alvand Dec 01 '22 at 00:03
I found that, some of you found that the IP address you get is :::1 or 0.0.0.1
This is the problem because of you try to get IP from your own machine, and the confusion of C# that try to return IPv6.
So, I implement the answer from @Johna (https://stackoverflow.com/a/41335701/812720) and @David (https://stackoverflow.com/a/8597351/812720), Thanks to them!
and here to solution:
add Microsoft.AspNetCore.HttpOverrides Package in your References (Dependencies/Packages)
add this line in Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // your current code // start code to add // to get ip address app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); // end code to add }
to get IPAddress, use this code in any of your Controller.cs
IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress; string result = ""; if (remoteIpAddress != null) { // If we got an IPV6 address, then we need to ask the network for the IPV4 address // This usually only happens when the browser is on the same machine as the server. if (remoteIpAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) { remoteIpAddress = System.Net.Dns.GetHostEntry(remoteIpAddress).AddressList .First(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork); } result = remoteIpAddress.ToString(); }
and now you can get IPv4 address from remoteIpAddress or result

- 7,073
- 7
- 61
- 71
-
1
-
7
-
2Using `remoteIpAddress.MapToIPv4();`, I get *0.0.0.1* value, not my machine address – Kiquenet Jun 01 '22 at 09:38
-
This is an acceptable solution. I employed this and got the correct IP after many trials. Thanks – Venugopal M Jul 06 '23 at 11:09
You can use the IHttpConnectionFeature
for getting this information.
var remoteIpAddress = httpContext.GetFeature<IHttpConnectionFeature>()?.RemoteIpAddress;

- 56,921
- 15
- 176
- 161
-
2Does it work for Kestrel hosting? In my demo, `httpContext.GetFeature
()` always be `null`. – Jerry Bian May 11 '15 at 03:01 -
2@JerryBian according to this doc: https://github.com/aspnet/Docs/blob/master/aspnet/fundamentals/servers.rst#supported-features-by-server, IHttpConnectionFeature is not supported in Kestrel (yet). – qbik Nov 12 '15 at 11:39
-
-
-
In ASP.NET 2.1, In StartUp.cs Add This Services:
services.AddHttpContextAccessor();
services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
and then do 3 step:
Define a variable in your MVC controller
private IHttpContextAccessor _accessor;
DI into the controller's constructor
public SomeController(IHttpContextAccessor accessor) { _accessor = accessor; }
Retrieve the IP Address
_accessor.HttpContext.Connection.RemoteIpAddress.ToString()
This is how it is done.
-
3
-
2
-
4What a terrible answer - IHttpContextAccessor is designed for injected services. The HttpContext is available as a property on Controller, and should be accessed as such. – slippyr4 Aug 10 '21 at 15:16
-
2@slippyr4 You aren't instantiating anything and you are still accessing the same thing as the controller. So unless there is some behind the scenes magic im not aware of you're rant here isn't warranted. This answer could be useful to people not using MVC – THE AMAZING Feb 20 '23 at 16:29
var remoteIpAddress = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress;

- 1,312
- 1
- 16
- 29
-
11Overly complicated. MVC already calls that internally and puts it under `HttpContext.Connection.RemoteIpAddress`. – Fred Apr 22 '16 at 12:28
-
-
This works for me (DotNetCore 2.1)
[HttpGet]
public string Get()
{
var remoteIpAddress = HttpContext.Connection.RemoteIpAddress;
return remoteIpAddress.ToString();
}

- 5,277
- 2
- 26
- 37

- 2,076
- 7
- 45
- 80
As of September 2021 - ASP.NET Core (5.x) MVC project allowed me to get the IP Address this way in my controller:
Request.HttpContext.Connection.RemoteIpAddress
Quite a bit more simple now than in the past, it seems.
Adding A Bit Of Clarity
I originally mentioned that this is available in a MVC project inside my HttpController.
The Request object is available because my DataController
is derived from the ASP.NET MVC Controller
class.
Here's snippet of Microsoft's base class code where that Request
and HttpRequest
is made available via inheritance to classes which derive from it.
#region Assembly Microsoft.AspNetCore.Mvc.Core, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60
// Microsoft.AspNetCore.Mvc.Core.dll
#endregion
#nullable enable
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ModelBinding.Validation;
using Microsoft.Net.Http.Headers;
namespace Microsoft.AspNetCore.Mvc
{
//
// Summary:
// A base class for an MVC controller without view support.
[Controller]
public abstract class ControllerBase
{
protected ControllerBase();
//
// Summary:
// Gets the Microsoft.AspNetCore.Http.HttpResponse for the executing action.
public HttpResponse Response { get; }
//
// Summary:
// Gets the Microsoft.AspNetCore.Http.HttpRequest for the executing action.
public HttpRequest Request { get; }

- 8,142
- 7
- 66
- 87
-
-
@user10191234 It does. Here is the official MS doc: https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.httprequest.httpcontext?view=aspnetcore-7.0 which shows that Request.HttpContext does indeed exist. Maybe the object you are accessing in your code isn't actually the HttpRequest. Also, see https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.connectioninfo.remoteipaddress?view=aspnetcore-7.0 for more info. – raddevus May 26 '23 at 19:54
In my case, I have DotNet Core 2.2 Web App running on DigitalOcean with docker and nginx as reverse proxy. With this code in Startup.cs I can get the client IP
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.All,
RequireHeaderSymmetry = false,
ForwardLimit = null,
KnownNetworks = { new IPNetwork(IPAddress.Parse("::ffff:172.17.0.1"), 104) }
});
::ffff:172.17.0.1 was the ip that I was getting before using
Request.HttpContext.Connection.RemoteIpAddress.ToString();

- 1,567
- 13
- 5
-
I think you probably done that, but it worth adding this piece of adivice for others here. When a docker container is restarted, it might get another lan ip address. A good practice is to tell your composer to use a specific IP address for that container as Lion said in his answer. – Huander Tironi Aug 04 '23 at 11:45
Running .NET core
(and probably .NET 6/7
) on IIS
behind a Load balancer did not work with other suggested solutions.
Manually reading the X-Forwarded-For
header does.
This code assumes the header contains one IP.
IPAddress ip;
var headers = Request.Headers.ToList();
if (headers.Exists((kvp) => kvp.Key == "X-Forwarded-For"))
{
// when running behind a load balancer you can expect this header
var header = headers.First((kvp) => kvp.Key == "X-Forwarded-For").Value.ToString();
// in case the IP contains a port, remove ':' and everything after
ip = IPAddress.Parse(header.Remove(header.IndexOf(':')));
}
else
{
// this will always have a value (running locally in development won't have the header)
ip = Request.HttpContext.Connection.RemoteIpAddress;
}
Edit: Thanks to @JawadAlShaikh
and @BozoJoe
for pointing out the IP can contain a port and the X-Forwarded-For
can contain multiple IPs.

- 5,932
- 2
- 32
- 57
-
3I found that `IPAddress.Parse(header)` will throw an error in case it contain port `ip:port` so a check should be made, or quick hack `IPAddress.Parse(header.Remove(header.IndexOf(':')))` – Jawad Al Shaikh Jul 22 '20 at 15:39
-
For reference, IPEndPoint.Parse effectively parses an IP Address and port – Laurent Jan 14 '21 at 20:29
-
1@JawadAlShaikh is correct. The values in X-Forwarded-For can contain `
:port` AND strings with mutiple values in them such as `192.168.1.1, 192.168.100.100` – BozoJoe Jun 09 '21 at 22:34 -
This code throws an exception when the header doesn't contain ':'. header.IndexOf(':') returns -1, which string.Remove() does not like. – Frank Hagenson Jul 13 '21 at 12:48
NOTE: When using localhost the IP address is always "0.0.0.1" but when I host the application on AWS EC2 instance using Nginx I receive the correct Ip address.
Add the below package to your project:
"Microsoft.AspNetCore.HttpOverrides": "2.2.0"
then add the following in the Configure() method in Startup.cs (Make sure to put it before app.UseStaticFiles() and app.UseRouting())
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
// app.UseStaticFiles();
// app.UseRouting();
and then in your controller class you can get the IP address using the following code:
IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress.MapToIPv4()?.ToString();

- 645
- 6
- 16
-
Don't know why this has so many upvotes, but a `string` cannot be convert to an `IPAddress`. Also, `RemoteIpAddress` can return a `null` value and needs to have a null check. – Codingwiz May 02 '23 at 16:19
As per the official documentation, if you are using Apache or Nginx integration, following code should be added to the Startup.ConfigureServices
method.
// using Microsoft.AspNetCore.HttpOverrides;
services.Configure<ForwardedHeadersOptions>(options =>
{
options.ForwardedHeaders = ForwardedHeaders.XForwardedFor |
ForwardedHeaders.XForwardedProto;
// Only loopback proxies are allowed by default.
// Clear that restriction because forwarders are enabled by explicit
// configuration.
options.KnownNetworks.Clear();
options.KnownProxies.Clear();
});
and then on top of everything, in Configure
method use
app.UseForwardedHeaders();
Further suppose in nginx conf file, inside a location, use
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
Now the first entry in the X-Forwarded-For
will be the real client IP.
IMPORTANT: If you want to secure the app and not allow an attacker inject X-Forwarded-For, Please read this answer.
Please see Forward the scheme for Linux and non-IIS reverse proxies, Configure Nginx and Dealing with invalid headers

- 607
- 7
- 15
In .NET 5 I use this to retrieve the client IP via a container on AWS fargate.
public static class HttpContextExtensions
{
//https://gist.github.com/jjxtra/3b240b31a1ed3ad783a7dcdb6df12c36
public static IPAddress GetRemoteIPAddress(this HttpContext context, bool allowForwarded = true)
{
if (allowForwarded)
{
string header = (context.Request.Headers["CF-Connecting-IP"].FirstOrDefault() ?? context.Request.Headers["X-Forwarded-For"].FirstOrDefault());
if (IPAddress.TryParse(header, out IPAddress ip))
{
return ip;
}
}
return context.Connection.RemoteIpAddress;
}
}
You call it like this:
var ipFromExtensionMethod = HttpContext.GetRemoteIPAddress().ToString();

- 7,172
- 5
- 55
- 69
First Add
Microsoft.AspNetCore.Http
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
in ConfigureServices
in Startup.cs
Then Add the following code in your controller
private IHttpContextAccessor _accessor;
public LoginController(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
public IEnumerable<string> Get()
{
var ip = _accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString();
return new string[] { ip, "value" };
}
Hope this will work for you

- 129
- 1
- 9
First, in .Net Core 1.0
Add using Microsoft.AspNetCore.Http.Features;
to the controller
Then inside the relevant method:
var ip = HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress?.ToString();
I read several other answers which failed to compile because it was using a lowercase httpContext, leading the VS to add using Microsoft.AspNetCore.Http, instead of the appropriate using, or with HttpContext (compiler is also mislead).

- 1,232
- 10
- 21
Running ASP.NET Core 2.1 behind a Traefik reverse Proxy on Ubuntu, I need to set its gateway IP in KnownProxies
after installing the official Microsoft.AspNetCore.HttpOverrides
package
var forwardedOptions = new ForwardedHeadersOptions {
ForwardedHeaders = ForwardedHeaders.XForwardedFor,
};
forwardedOptions.KnownProxies.Add(IPAddress.Parse("192.168.3.1"));
app.UseForwardedHeaders(forwardedOptions);
According to the documentation, this is required if the reverse proxy is not running on localhost. The docker-compose.yml
of Traefik has assigned a static IP address:
networks:
my-docker-network:
ipv4_address: 192.168.3.2
Alternatively, it should be enough to make sure a known network is defined here to specify its gateway in .NET Core.

- 16,606
- 23
- 86
- 148
Short version of @crokusek's answer
public string GetUserIP(HttpRequest req)
{
var ip = req.Headers["X-Forwarded-For"].FirstOrDefault();
if (!string.IsNullOrWhiteSpace(ip)) ip = ip.Split(',')[0];
if (string.IsNullOrWhiteSpace(ip)) ip = Convert.ToString(req.HttpContext.Connection.RemoteIpAddress);
if (string.IsNullOrWhiteSpace(ip)) ip = req.Headers["REMOTE_ADDR"].FirstOrDefault();
return ip;
}

- 10,970
- 6
- 59
- 64
try this:
string remoteHost = $"{httpContext.Connection.RemoteIpAddress}:{httpContext.Connection.RemotePort}";

- 21
- 1
- 4
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 28 '22 at 12:54
You can also get IP from an external service.
public string GetIP()
{
HttpClient client = new HttpClient();
var result = client.GetStringAsync("https://jsonip.com/").Result;
var ip = JsonSerializer.Deserialize<RemoteIPDto>(result.ToString()).IP;
return ip;
}
Where RemoteIPDto
class is
public class RemoteIPDto
{
[JsonPropertyName("ip")]
public string IP { get; set; }
[JsonPropertyName("geo-ip")]
public string GeoIp { get; set; }
[JsonPropertyName("API Help")]
public string ApiHelp { get; set; }
}

- 1,620
- 23
- 37
From this link, there is a better solution.
In Startup.cs, we need to add service-
public void ConfigureServices(IServiceCollection services)
{
........
services.AddHttpContextAccessor();
........
}
Then in any controller or any place, we need to use it via dependency injection like this-
private IHttpContextAccessor HttpContextAccessor { get; }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, IWebHostEnvironment env, IHttpContextAccessor httpContextAccessor)
: base(options)
{
Environment = env;
HttpContextAccessor = httpContextAccessor;
//this.Database.EnsureCreated();
}
And then get IP like this-
IPAddress userIp = HttpContextAccessor.HttpContext.Connection.RemoteIpAddress;

- 13,970
- 24
- 112
- 161
You can also get both IP Address and the location using the api.db-ip.com
web service.
The service is free, but it has a limitation: 1,000 requests per day.
public string GetRemoteIP()
{
HttpClient client = new HttpClient();
var result = client.GetStringAsync("https://api.db-ip.com/v2/free/self").Result;
var ip = JsonSerializer.Deserialize<RemoteIPDto>(result.ToString()).IP;
return ip;
}
public static string GetUserAddress()
{
HttpClient client = new HttpClient();
var result = client.GetStringAsync("https://api.db-ip.com/v2/free/self").Result;
var location = result.ToString();
return remoteAddress;
}
where RemoteIPDto
class is
public class RemoteIPDto
{
[JsonPropertyName("ipAddress")]
public string IP { get; set; }
[JsonPropertyName("continentCode")]
public string ContinentCode { get; set; }
[JsonPropertyName("continentName")]
public string ContinentName { get; set; }
[JsonPropertyName("countryCode")]
public string CountryCode { get; set; }
[JsonPropertyName("countryName")]
public string CountryName { get; set; }
[JsonPropertyName("city")]
public string City { get; set; }
}

- 73
- 5
I am using asp.net core 6 and this worked for me
var remoteIpAddress = HttpContext.Connection.RemoteIpAddress;
as brothers said before it will give you the local ip address but you will note the difference when you deploy your web application to a server.

- 38
- 4
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
public string GetClientIPAddress(HttpContext context)
{
string ip = string.Empty;
if (!string.IsNullOrEmpty(context.Request.Headers["X-Forwarded-For"]))
{
ip = context.Request.Headers["X-Forwarded-For"];
}
else
{
ip = context.Request.HttpContext.Features.Get<IHttpConnectionFeature>().RemoteIpAddress.ToString();
}
return ip;
}
Where you want to get Ip address;
GetClientIPAddress(HttpContext);

- 318
- 2
- 7
To get IP address and hostname in .NET Core, put the following code in the controller:
var addlist = Dns.GetHostEntry(Dns.GetHostName());
string GetHostName = addlist.HostName.ToString();
string GetIPV6 = addlist.AddressList[0].ToString();
string GetIPV4 = addlist.AddressList[1].ToString();

- 9,564
- 146
- 81
- 122

- 37
- 1
-
1doesn't this get the IP of the host machine? looking for the client IP – Ryan Vettese Sep 23 '20 at 17:02
-
1
-
Agreed with others. This will not help the developers in getting the client address. – Sudhakar Chavali Jan 03 '22 at 09:04
try this.
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
ipAddress = ip.ToString();
}
}

- 568
- 6
- 16