Click here to Skip to main content
12,894,200 members (55,961 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# ASP.NET
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;
               str = IpAddress();
               Session["ip"] = str;
               Label1.Text = Session["ip"].ToString();
              Label1.Text = Session["ip"].ToString();

i have tested on local host
it is giving o/p
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
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Of course it's giving you! 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.
maan_k 15-Feb-13 10:02am
does it means my code is correct.will it work fine when the site hosted?
Dave Kreskowiak 15-Feb-13 12:46pm
He's a wacked idea: TEST IT AND SEE FOR YOURSELF!
Logi Guna 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
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();

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[^]

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100