Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get user ip address for my application. I am using following code as
C#
txtip.Text = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

but it always return 127.0.0.1 but my actual ip address is 192.0.0.110. I want this ip to be returned using coding in asp.net .

Please any one help me soon. I have to finish the task.

Thanks in advance.
Posted
Updated 4-Oct-12 19:25pm
v2

Try this:
C#
string visitorIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (String.IsNullOrEmpty(visitorIPAddress))
    visitorIPAddress = Request.ServerVariables["REMOTE_ADDR"];

or
C#
if (string.IsNullOrEmpty(visitorIPAddress))
    visitorIPAddress = Request.UserHostAddress;



--Amit
 
Share this answer
 
This code results exact ip address of system. rather than the router address like 127.0.0.1
C#
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].GetIPProperties().UnicastAddresses[0].Address.ToString();

happy coding.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900