Today in this article, we will learn how to read or Get Client IP addresses in ASP.NET Core programmatically in ASP.NET Core.
This could be a legitimate requirement for your application where you may want to record the IP address of the Client using your restricted resources.
We shall see how to get IP addresses locally and applications that are hosted in IIS or hosted behind Load balancers etc.
We will cover below aspects in the article,
Please note that due to Proxy servers or Load balancers, request data could be removed before reaching the client application.
In such cases, you must forward the client IP address in a header by some means which we shall see in today’s article.
In this article, however, we will only be dealing with client IP address retrieval but you may look at similar techniques to address other requirements like Redirect, Authentication, Policy authorization, etc.
Getting started
Kindly add Microsoft.AspNetCore.HttpOverrides Nuget package to your project references. This step is needed if you are using ASp.NET Core 3.1 and lower packages.
This package is available for ASP.NET Core 6.0 and above as inbuilt (within the framework.)
Using Nuget Package Manager,
PM> Install-Package Microsoft.AspNetCore.HttpOverrides
OR
Using .NET CLI
> dotnet add package Microsoft.AspNetCore.HttpOverrides
Note: Please use the latest available version.
Add UserForwardedHeaders middleware in the API Pipeline
Please add UserForwardedHeaders middleware in the API Pipeline by updating Configure and ConfigureServices methods in Startup.cs.
Update the Configure method as below,
Forwarded Headers Middleware execution order important – Forwarded Headers Middleware should run before any other middleware.
If no middleware is specified then default headers will be set as ForwardedHeaders.None
The above setting is recommended when using a reverse proxy server.
In the Header option,
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
ForwardedHeaders.XForwardedFor – Identifies which forwarders should be processed.
ForwardedHeaders.XForwardedProto – The middleware updates the Request.Scheme for security policies to work properly.
If you are using a known reverse proxy or network then ForwardedHeadersOptions can be updated for those configurations as below,
Get IP Address in Controller
With the above settings, you should be all set to get an IP address using the below code,
IPAddress remoteIpAddress = Request.HttpContext.Connection.RemoteIpAddress;
Get IP Address in Other Module or classes
You can get access to IP Addresses in other Modules or Classes by using the interface IHttpContextAccessor. Please see this article for more details.
If running the service locally localhost (::1 / 127.0.0.1), Additionally, I had to use the below code to read the actual IP address locally.
References:
Once executed above method will give the client an IP address successfully.
That’s All!
Do you have any comments or ideas or any better suggestions to share?
Please sound off your comments below.
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
this worked perfectly, thanks!!
Hi Pandita – Glad the article helped you! Thanks for the feedback!
Hi
I Am working with asp.net core 5.0 mvc on vs2022 community edition. Has there been a change from 3.1 I can’t get the ‘request” portion to resolve. What name space or package needs to be installed?
Thanks
i done above thinks in my code but still i am not able to get proper client ip address dotnet core.
I can concur this works without it you cannot call request.HttpContext.Connection.RemoteIpAddress;
Thanks Robbie. Glad the article helped you.