Client IP using C#
Solution 1
HttpContext.Current.Request.UserHostAddress
This doesn't attempt to take into account proxies. For that, you can use Request.ServerVariables["HTTP_X_FORWARDED_FOR"]
. However, make sure you're not trusting that blindly, since it could be forged. It's better to keep a whitelist of IPs for which you trust it.
Solution 2
String clientIP =
(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]==null)?
HttpContext.Current.Request.UserHostAddress:
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
Nithesh Narayanan
I am Nithesh Adukkam Narayanan, a Full stack developer and Architect with 11+ years of experience in software development, design, best practices, and mentoring. My primary skillset in the .Net tech stack (C#, .net, .net core, Web API, MVC) along with the front end (React JS, JavaScript, Typescript, webpack). Been an innovator and played a key role in R&D projects using python, image processing, and ICR. Proven expertise in cutting-edge technologies like cloud (azure, aws), event-driven, micro-service architecture, micro front end architecture, Docker, and containers. Hands-on in CI/CD (Azure DevOps) and agile methodologies.
Updated on June 28, 2022Comments
-
Nithesh Narayanan almost 2 years
How can i get IP address of client machine in C#.? I want to keep a log register for my online application and to keep IP address of logging system i want to get the IP address of client....
Advance Thanks...