Click here to Skip to main content
15,993,109 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All ,

I want to save the ip address how hits my site, so help me how to get it.

I don't want server IP address just want local machine IP which access my site.

What I have tried:

I Used this code

C#
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
    if (ip.AddressFamily == AddressFamily.InterNetwork)
    {
        lblip.Text = ip.ToString() ;
        tblUserIP test = new tblUserIP();
        test.IPAddress = ip.ToString();
        test.UserID = LoginID;
        test.LoginTime = System.DateTime.Now;
        obj.tblUserIPs.Add(test);
    }



but it gives Server addres..
Posted
Updated 11-Mar-16 1:09am
v2
Comments
Jochen Arndt 11-Mar-16 3:20am    
Dns.GetHostName() returns the name of the system on which the program is executed. This will be the name of your server when executed there.

"I want to save the ip address how hits my site, so help me how to get it."

So you want to know the IP addresses of the clients connecting to your server?

You should tell us what kind of service is running on your system. If the service has been written by you, it should be no problem to get the IP addresses of the clients that connected to the service. If not, the service may have an option to write this information to a log file.

Assuming by "site" you mean an ASP.NET site, Request.UserHostAddress[^] is the property you're looking for.
 
Share this answer
 
Hi All, Tanks for your reply,I fond the solution fro the above problem
I used below code and work for me

C#
System.Web.HttpContext context = System.Web.HttpContext.Current;
           string ipAddress = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

           if (!string.IsNullOrEmpty(ipAddress))
           {
               string[] addresses = ipAddress.Split(',');
               if (addresses.Length != 0)
               {
                   return addresses[0];
               }
           }

           return context.Request.ServerVariables["REMOTE_ADDR"];
 
Share this answer
 
Hi,

Follow the below link.

Obtaining local IP address

Check Local IP Address [C#]

Thanks,
Sisir Patro
 
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