Click here to Skip to main content
15,896,489 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Hello Friend

I made a one login panel in which i want to save a user ip address when user login my panel.
so i want to know which particular ip i have to save.

As i know there is a public ip but it is change every time and static ip is so costly so normal people can't use it.so people take a public ip which is provided by BSNL or any other internet provider so i am confuse which ip address i have to save.

So please tell me that which ip address i have to save to trace particular user.
Posted
Updated 24-Dec-13 1:50am
v2
Comments
[no name] 24-Dec-13 7:55am    
You don't have a big choice I think. Either it is static or usually dynamic from the provider.
http://www.aspsnippets.com/Articles/How-to-get-IP-Address-of-Visitors-Machine-in-ASP.Net.aspx

You can't really "trace" it easily - you have access to the public IP directly via the UserHostAddress property[^] - but that's about as far as you can go.
Do note that this identifies the router they are using to connect, not the individual PC, Tablet or phone - you can't get that IP address (and it wouldn't mean anything anyway as it is only unique on the local network segment the user is working on)
 
Share this answer
 
Try below code to get local ip address


C#
public string LocalIPAddress()
 {
   IPHostEntry host;
   string localIP = "";
   host = Dns.GetHostEntry(Dns.GetHostName());
   foreach (IPAddress ip in host.AddressList)
   {
     if (ip.AddressFamily == AddressFamily.InterNetwork)
     {
       localIP = ip.ToString();
       break;
     }
   }
   return localIP;
 }
 
Share this answer
 
Comments
Rohit Sharma706 24-Dec-13 8:02am    
With this local ip address i can trace the user in future.
 
Share this answer
 
By using this i solved my problem

This code working on live not in locally
So after apply this code upload into server.
C#
private string GetPublicIpAddress()
   {
       String aip = (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null) ? HttpContext.Current.Request.UserHostAddress : HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
       return aip;
   }
 
Share this answer
 
v2
C#
try this



            string ip = "0";
            ip = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (!string.IsNullOrEmpty(ip))
            {
                string[] ipRange = ip.Split(',');
                ip = ipRange[0].Trim();
            }
            else
            {
                ip = Request.ServerVariables["REMOTE_ADDR"];
            }
 
Share this answer
 
v2
Try

C#
string IPAddress = "";

String strHostName = HttpContext.Current.Request.UserHostAddress.ToString();

IPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();
 
Share this answer
 

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