Wednesday, May 7, 2008

Detecting User's IP Address

If your website contains sensitive data and you don't want any spamming or any malicious use of your website ,then one thing you can do is to keep a track of the visiting user's system IP and this can be done in some easy steps given below,


string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();


Now set the value of clientIPAddress to either a Textbox or label or any control as per your requirement.

6 comments:

Anonymous said...

You could just use Request.UserHostAddress...

Talib Ali Khan said...

Hi Timothy,
Request.UserHostAddress shows the IP host address 127.0.0.1 and not the required Client's IP... :-)

Anonymous said...

That's only if you're running your web site on your local machine (http://localhost...)

Your code will detect the machine's IP address (the one that is running the web app)... that's cool too :)

Peace,
-Timothy

Anonymous said...

hi,

I want the ipaddress and computername of the the client who is accessing the website. when my website is deployed on client machine how to get ipadd.

alen

Talib Ali Khan said...

Hi,

Sorry for late reply 'coz a li'l busy :)

For Capturing IP , try this

For eg.,

string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(2).ToString();
Label2.Text = clientIPAddress;


OR

string strHostName = System.Net.Dns.GetHostName();
string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
Label2.Text = clientIPAddress;



For Computer Name , try this
For eg.,
Label1.Text=System.Net.Dns.GetHostByName(strHostName).HostName;

Anonymous said...

how can trace website visitor user system IP and Name of the computer of that system?
Note: i am not asking host address(dns)
Request.UserHostAddress shows the IP Shows Hosting server IP only.
I need website visitors IP-Address
any one please help out from this...

Advance Thanks