2

I developed site. I need to get IP of site visitors. I try to use Request, but it have only internal IP:

Response.Write(Request.ServerVariables["REMOTE_ADDR"]);

I looked all keys in Server Variables collection - the same result:

foreach (string var in Request.ServerVariables)
{
    Response.Write(Request[var]);
}

How can I get external IP address?

शेखर
  • 17,412
  • 13
  • 61
  • 117
Elena
  • 73
  • 1
  • 2
  • 9
  • I found the problem. I used VPN connetion, that's why ServerVariables returns me internal IP. I closed VPN connection and it returned me correct external IP. – Elena Apr 12 '13 at 06:48

5 Answers5

11

You can use it to get External (public) IP Address..

public static string getExternalIp()
    {
        try
        {
            string externalIP;
            externalIP = (new WebClient()).DownloadString("http://checkip.dyndns.org/");
            externalIP = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"))
                         .Matches(externalIP)[0].ToString();
            return externalIP;
        }
        catch { return null; }
    }
Imran Athar
  • 469
  • 5
  • 8
2

This it part of my implementation for WebTracking. So here you go a snippet for IpAddress retrieval. You can read more about the Server Variables at Wikipedia.

    /// <summary>
    /// Get ip address from request
    /// </summary>
    /// <param name="request"></param>
    /// <returns></returns>
    private string GetIpAddress(HttpRequest request)
    {
        if (request.ServerVariables.IsNull()) return null;

        var _realAddress = request.ServerVariables[@"HTTP_X_FORWARDED_FOR"];
        if (_realAddress.IsNullOrEmpty())
        {
            _realAddress = request.ServerVariables[@"HTTP_FORWARDED"];
        }
        if (_realAddress.IsNullOrEmpty())
        {
            _realAddress = request.ServerVariables[@"REMOTE_ADDR"];
        }

        return _realAddress;
    }
1

Are you behind a load-balancer/proxy? If so, inspect the headers collection of the request for headers similar to X-FORWARDED-FOR.

spender
  • 117,338
  • 33
  • 229
  • 351
1

You can use like this

String lstrIpAddress;
if (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null || Request.ServerVariables["HTTP_CLIENT_IP"] != null)
    lstrIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
else
    lstrIpAddress = Request.ServerVariables["REMOTE_ADDR"];
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • It doesn't help. Request.ServerVariables["HTTP_X_FORWARDED_FOR"] - returns null and Request.ServerVariables["REMOTE_ADDR"] returns internal IP – Elena Apr 11 '13 at 13:13
  • This only works for ASP pages (which is what the question asks, but I thought I'd mention this). – AStopher Dec 05 '16 at 00:05
1

you can simply use this nuget package. https://www.nuget.org/packages/IpPublicKnowledge/

easy and simple. here is an example :

// Get Public IP
            var ip = IPK.GetMyPublicIp();

            //Get all IP infos
            var IPinfo = IPK.GetIpInfo(ip);

            //print some info
            Console.WriteLine("*--------------------------- IPK -----------------------------*");

            Console.WriteLine("My public IP : " + IPinfo.IP);
            Console.WriteLine();
            Console.WriteLine("My ISP : " + IPinfo.isp);
            Console.WriteLine();
            Console.WriteLine("My Country : " + IPinfo.country);
            Console.WriteLine();
            Console.WriteLine("My Languages : ");

            foreach (var lang in IPinfo.languages)
            {
                Console.WriteLine(" " + lang.Key + " : " + lang.Value);
            }

            Console.WriteLine("*-------------------------------------------------------------*");
            Console.Read();
Driss BIYA
  • 31
  • 3