locked
Cannot capture my laptop ip when logging IP from asp.net website RRS feed

  • Question

  • User1549208114 posted

    Dear List, I've been trying to get my laptop IP to log on my Azure website without any success.

    Things work fine when running locally but after publishing, I think I can only get the Azure server IP.

    I've tried everything I could find on the web.

    1:  

    //string looky1 = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                //string looky2 = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

    2: 

    //string myExternalIP;

                //string strHostName = System.Net.Dns.GetHostName();

                //string clientIPAddress = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString();

                //string clientip = clientIPAddress.ToString();

                //System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("http://www.whatismyip.org");

                //request.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE" + "6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

                //System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

                //using (System.IO.StreamReader reader = new StreamReader(response.GetResponseStream()))

                //{

                //    myExternalIP = reader.ReadToEnd();

                //    reader.Close();

                //}

                //this.tbxLogonStatus.Text = "IP = " + myExternalIP.ToString();

    3: 

    private string GetIPInfo()
    {
    string ipString = "";

    // Get a list of all network interfaces (usually one per network card, dialup, and VPN connection)
    NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();

    foreach (NetworkInterface network in networkInterfaces)
    {
    // Read the IP configuration for each network
    IPInterfaceProperties properties = network.GetIPProperties();

    // Each network interface may have multiple IP addresses
    foreach (IPAddressInformation address in properties.UnicastAddresses)
    {
    // We're only interested in IPv4 addresses for now
    //if (address.Address.AddressFamily != AddressFamily.InterNetwork)
    if (address.Address.AddressFamily.ToString().Length == 0 )
    continue;

    // Ignore loopback addresses (e.g., 127.0.0.1)
    if (IPAddress.IsLoopback(address.Address))
    continue;

    ipString += address.Address.ToString() + " (" + network.Name + ")";
    }
    }

    return ipString;
    }

    They all worked locally but none of them successfully returned my laptop IP from Azure.

    I want to log customer ip's to look for bad behavior on my web site.

    Help appreciated.

    Thanks

    Thursday, June 18, 2015 5:57 PM

All replies

  • User753101303 posted

    Hi,

    And what happens when using #1? Note that you won't see necessarily your IP address but how it is exposed on the internet (ie the external web address for your proxy). Don't you have the same thing than when looking at https://www.whatismyip.com/ ?

    #2 and #3 is to read information about the local machine (so it just appears to work when the client and the server machine are the same,machine).

    Also this kind of basic information should be already tracked in the IIS log depending on your exact final goal.

    Friday, June 19, 2015 3:51 AM
  • User1549208114 posted

    This is very helpful information. I'll investigate the IIS angle first. 

    THANKS!

    Friday, June 19, 2015 11:44 AM