Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI
how to get the public ip address of a client machine using asp.net. I have tested many code which are given in many sites, but it shows only the ip address of client machine which is given in a LAN but not the real public ip address.

thanks
Posted

try this code..

C#
protected void GetUser_IP()
{
    string VisitorsIPAddr = string.Empty;
    if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
    {
        VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
    }
    else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
    {
        VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
    }
    uip.Text = "Your IP is: " + VisitorsIPAddr;
}


Updated

see this link

Getting the External IP Address[^]
 
Share this answer
 
v2
Comments
Jijutj 20-Dec-13 0:52am    
thanks for replying, but this code will not return the public ip address. It will return only the ip address which is assigned in a LAN.
An@nd Rajan10 20-Dec-13 0:59am    
see the updated solution, that is may helps you !!!
http://stackoverflow.com/questions/19285957/how-to-get-public-ip-address-of-a-user-in-c-sharp
 
Share this answer
 
Comments
Jijutj 20-Dec-13 0:55am    
thanks for replying, but this code will not return the public ip address. It will return only the ip address which is assigned in a LAN.
You will only be able to get the public IP address and not their local address. (firewalls are made for this reason)

As far as getting a computer's private IP and name you can not do this via ASP.NET either. You would need to drop an ActiveX control or some sort of other application on the users machine to do so which most anyone would not allow as you have pointed out.

When it comes to who accessed the application your best bet would be to implement some sort of membership into your application and track when they log into the application itself
 
Share this answer
 
Comments
Jijutj 20-Dec-13 1:40am    
just have a question, google for example, shows the account activity page, where we see the ip address. how we can do this using asp.net?

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