Please enable Javascript for better experience...
How to get User IP-Address using C#
By Rahul Kumar Jha | Jan 29, 2015 | In Tips | Total Views [ 4613 ]
Taged In
(0 Like)
Rate

Using below code you can fetch User's system IP-Address.

The Problem

Sometime you want to fetch user's IP-Address for monitoring purpose. You can easily get it from current HTTP request. Let's see how to do this.

A quick solution

Simply put this code in your code. Below example useHttpContext.Current.Request to get Current HTTP request.

You can find HttpContext.Current.Request in System.Web namespace.

Code

    public class Utility
    {
        public string GetSystemIPAddress()
        {
            return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

    }

Implementation

Let's call this method.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("System IP-Address is:" + new Utility().GetSystemIPAddress());
        }
    }

Hope this can help you.

Share this

About the Author

Rahul Kumar Jha
Rahul Kumar Jha
Founder, Developer dotnet-concept.com

Public profile: user/profile/99900001


Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, ANGULAR, ANGULAR JS, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

User's Comments


 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment
 
 
 
 
 
 
Sponsors