C# get ip address (Client IP Address Asp.net)

C# get IP address (Client IP Address Asp.net)

In this tutorial, I will discuss "C# get IP address (Client IP Address Asp.net)". There are several methods to find clients or visitor's IP addresses. Asp.net provides us, inbuilt class, and function by which we easily find the IP address of the client's system.

Every time when you developed a software there is a requirement for the IP address just because we can track the end-users IP address and identify who actually doing the data modification. It is a good practice to implement IP address concept in your application.

Before Implementing the IP address code in your application. Let's know how to check IP address in your system.

How to check the IP address
Step 1. Open command prompt by pressing window key + R and enter cmd and search.
Step 2. Type ipconfig to know your actual IP address. See below img.

C# get ip address (Client IP Address Asp.net)
Get Client Ip in Asp.net C#

There are many ways you can find out IP address in c# asp.net, some are working on the local server and some are working on the remote server. The method is given below, follow the step to get IP address.

Method 1 (For the Server IP Address)

The first method is very simple and easy, you just need to write only one line of code. 
Note: This method does not give actual IP address in the local server if want to see IP address use remote server.

public string getIpAddress()
string ipAddress = HttpContext.Current.Request.UserHostAddress.ToString();
return ipAddress;

Method 2  (For the Visitor IP Address)

This method is actually the very best way to find the IP address it will work on both servers local as well as remote server/ production server. Don't forget to use these Name Spaces.

using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

public static string GetLocalIPAddress()
        var host = Dns.GetHostEntry(Dns.GetHostName());
        foreach (var ip in host.AddressList)
            if (ip.AddressFamily == AddressFamily.InterNetwork)
                return ip.ToString();
        throw new Exception("No network adapters with an IPv4 address in the system!");

My recommendation is to use Method 2 to get the proper IP address. I hope you will like this C# get ip address post.


  1. I am getting a exception in Blazer System.PlatformNotSupportedException: System.Net.NameResolution is not supported on this platform.


Post a Comment

Post a Comment

Previous Post Next Post