Capturing the user's real IP

4

How do I capture the real IP / Public IP of the user who is accessing the application? The IP of the computer I can capture normally, but I want to know the public IP of that client.

    
asked by anonymous 03.09.2015 / 22:23

3 answers

4

This is an accessible variable in any < in> Action :

Request.UserHostAddress;

Or else:

Request.ServerVariables["REMOTE_ADDR"]
    
03.09.2015 / 22:30
4

I get it like this:

        public static string GetPublicIP()
        {
            string url = "http://checkip.dyndns.org";
            System.Net.WebRequest req = System.Net.WebRequest.Create(url);
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            string response = sr.ReadToEnd().Trim();
            string[] a = response.Split(':');
            string a2 = a[1].Substring(1);
            string[] a3 = a2.Split('<');
            string a4 = a3[0];
            return a4;
        }
    
03.09.2015 / 23:20
2

add using

using System.Net;                

include the code below

string host = Dns.GetHostName();
string ip = Dns.GetHostAddresses(host)[2].ToString();
    
07.07.2017 / 15:12