0

Hi i'm working on logging on a security website in school, but it's all localhost based.

I'm looking to actually capture whoever visits my website. I actually want to capture the external IP address of whomever machine the person is using. For example, when i run my website and hit a button it immediately captures my external ip address.

To be frank, i have no much knowledge in this area of ip address but i have sufficient understanding on what are things like client, server, ip address, port numbers, etc.. which i learnt in my networking modules.

To give more information, when you visit website to check IP address or any other what is your ip website, you get your external ip address. I actually want to capture this IP address and save it into my database.

I did do my research on stackoverflow, codeproject, whichever that i could find help from, but it always return either ::1, which i pressume is 127.0.0.1 our default localhost address?

I have tried all these down below to experiment what they produce..

protected void getIP_Click(object sender, EventArgs e)
{
    String hostName = System.Net.Dns.GetHostName();
    String clientIpAddressHostName = System.Net.Dns.GetHostAddresses(hostName).GetValue(0).ToString();
    IP1.Text = clientIpAddressHostName;

    String clientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_HOST"].ToString();
    IP2.Text = clientIpAddress;

    String ip = null;

    if (String.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]))
    {
        ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    IP3.Text = ip;

    String ipNext = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
    IP4.Text = ipNext;

    String ipNextNext = HttpContext.Current.Request.UserHostAddress;
    IP5.Text = ipNextNext;

    String last = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
    IP6.Text = last;
}

I also read online that i can actually get my external ip address from an external source from a website based on this answer down below from stackoverflow

Here

But i havent tried it, as i assume this isn't a safe way for doing? Please correct me if i'm wrong..

Still cant get what im looking for after searching high and low..

Could anyone please assist? Would appreciate any help!

domster
  • 556
  • 2
  • 8
  • 26

1 Answers1

0

Looks like you are reading your Server IP address.

The Client IP address can be read via: Http​Request.​User​Host​Address

The IP address you receive in your Server would be the "public" address of the Client. i.e., if the user is behind a proxy, you would get the proxy address.

If you are using IIS to host your web site, you can configure it to log the Client IP address. In this case, you need not write any code to log IP address.

Configure IIS Logging

In the W3C Logging Fields dialog box, select one or more of the following options:

Client IP Address (c-ip): the IP address of the client that made the request.

Community
  • 1
  • 1
Subbu
  • 2,130
  • 1
  • 19
  • 28
  • thanks for your help @Subbu.. i dont think i'd want to step into the area of configuring the iis, i have no knowledge on that at all.. i'll try using the line of code you provided and see what i get and get back here. thank you! – domster Jul 23 '17 at 12:22
  • Sorry to correct my output.. I got this when i user HttpRequest.UserHostAddress - ::1 <- im guessing it's our default assigned address for localhost? – domster Jul 23 '17 at 12:50
  • Try to access your web site from an external network. For e.g., access from home (without any VPN) – Subbu Jul 23 '17 at 13:30
  • I tried this method actually.. https://www.codeproject.com/Tips/452024/Getting-the-External-IP-Address And i got 219.xxx.xxx.xxx which is what i'm looking for.. But is this way secured and safe? Getting my IP address from an external site. – domster Jul 23 '17 at 13:39