Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
friends,
i have used the following code for checking ip address but it is not giving exact ip address
---------------------------------------------------------------------------------------------
public string IpAddress()
      {
          string strIpAddress;
          strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
          if (strIpAddress == null)
          {
              strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
          }
          return strIpAddress;
      }
      protected void btnipcheck_Click(object sender, EventArgs e)
      {
          string str;
          try
          {
               str = IpAddress();
               Session["ip"] = str;
               Label1.Text = Session["ip"].ToString();
          }
          catch
          {
              Label1.Text = Session["ip"].ToString();
          }
      }
------------------------------------------------------------------------------------------
i have tested on local host
it is giving o/p 127.0.0.1
but my ip address is different :10.25.---.---
---------------------------------------------------------------------------------
so what is the other code for getting the exact ip address of client machine.
Posted 15-Feb-13 3:03am
maan_k446
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Of course it's giving you 127.0.0.1! You're testing it on your DEV machine! The request doesn't go out to the internet and back in so it's not going to use the 10.x.x.x route.
 
If you want the IP the request came from, you'll have to run the site on a seperate machine. Then you can make a request from your machine and get the real IP address.
  Permalink  
Comments
maan_k at 15-Feb-13 10:02am
   
does it means my code is correct.will it work fine when the site hosted?
Dave Kreskowiak at 15-Feb-13 12:46pm
   
He's a wacked idea: TEST IT AND SEE FOR YOURSELF!
Logi Guna at 16-Feb-13 2:02am
   
strIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
that's working for me to get client IP.
of course you must host your site and browse from another machine.
My 5 for Dave.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I use this in a Windows EXE program:
string strHostName = System.Net.Dns.GetHostName();
string strIPAddress = "";
System.Net.IPAddress[] objAddressList = System.Net.Dns.GetHostEntry(strHostName).AddressList;
for (x = 0; x <= objAddressList.GetUpperBound(0); x++) 
{
    if (objAddressList[x].AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) 
    {
        strIPAddress = objAddressList[x].ToString();
        break; 
    }
}
 

 
I found this via Google Search at get ip address in asp page - client side[^]
Response.Write("Local Machine Host name is "+Dns.GetHostName()+"<br>");
         string hostname = Dns.GetHostName();
         IPHostEntry ipEntry = Dns.GetHostByName(hostname);
         IPAddress[] addr = ipEntry.AddressList;
         for (int i = 0; i < addr.Length; i++)
           {
               Response.Write(string.Format("IP Address {0}: {1} ", i, addr[i].ToString()) + "<br>");
           }
 
And some good tips at A Gotcha Identifying the User's IP Address[^]
  Permalink  
v4

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

  Print Answers RSS
0 OriginalGriff 425
1 DamithSL 218
2 Dave Kreskowiak 200
3 Maciej Los 195
4 arvind mepani 195


Advertise | Privacy | Mobile
Web01 | 2.8.140709.1 | Last Updated 15 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid