Home > DeveloperSection > Forums > How to get client IP address
Kenny Tangnde

Total Post:68

Points:476
Posted on    September-29-2011 7:50 PM

 C# C# 
Ratings:


 7 Reply(s)
 1725  View(s)
Rate this:

Hi all,

How to get client IP address,And will IP address stored in the database

thanks in advance.



Mark Toy
Mark Toy

Total Post:7

Points:49
Posted on    September-30-2011 4:28 AM

Why you want to get client IP address?

Kenny Tangnde

Total Post:68

Points:476
Posted on    September-30-2011 10:57 PM

for security,
Consider the user's account unusual, such as remote login

Awadhendra Tiwari

Total Post:126

Points:882
Posted on    October-01-2011 12:38 AM

Hi Aken,

If you want to know client ip address in asp.net then you can use following code
Request.UserHostAddress()   ---> It will give you client address

And if you want it in c# then you can write following code
   
IPAddress[] addresses = Dns.GetHostByName( "MACHINE1" ).AddressList;

foreach( IPAddress address in addresses )
{
    Console.WriteLine( address.ToString() );
}

Thanks.

Kenny Tangnde

Total Post:68

Points:476
Posted on    October-01-2011 1:07 AM

Hi Awadhendra Tiwari,

 thanks for your helping.

using System.Net;

namespace GetClientIPAddress

{
    class Program
    {
        static void Main(string[] args)
        {
            //GetHostByName(this parameter  is what).

        IPAddress[] addresses = Dns.GetHostByName("").AddressList; //If only want IP. How to do that?

       Console.Write("Your IP Address is:");
            foreach (IPAddress address in addresses)
            {

                Console.WriteLine(address.ToString());
            }
            Console.ReadKey(true);
        }
    }
}


Kenny Tangnde

Total Post:68

Points:476
Posted on    October-01-2011 1:21 AM

Hi Awadhendra Tiwari,

How get the local computer name.

Thanks.


Awadhendra Tiwari

Total Post:126

Points:882
Posted on    October-01-2011 3:16 AM

Hi...
You can use following line of code to get machine name.

Console.WriteLine(Environment.MachineName);

Thanks.

Kenny Tangnde

Total Post:68

Points:476
Posted on    October-01-2011 3:47 AM

Hi Awadhendra Tiwari,
My problem has been solved, thank you for your help.

Don't want to miss updates? Please click the below button!

Follow MindStick