views:

207

answers:

6

Am I able to depend on a requestor's IP coming through on all web requests?

I have an asp.net application and I'd like to use the IP to identify unauthenticated visitors. I don't really care if the IP is unique as long as there is something there so that I don't get an empty value.

If not I guess I would have to handle the case where the value is empty.

Or is there a better identifier than IP?

+1  A: 

Well, web request is an http connection, which is a tcp connection and all tcp connections have two endpoints. So, it always exists. But that's about as much as you know about it. It's neither unique nor reliably accurate (with all the proxies and stuff).

Michael Krelin - hacker
+2  A: 

I believe that this value is set by your web sever and there is really no way to fake it as your response to there request wouldn't be able to get back to them if they set there IP to something else.

The only thing that you should worry about is proxies. Everyone from a proxy will get the same IP.

William Clemens
A: 

IP address is not much use for identifying users. As mentioned already corporate proxies and other private networks can appear as a single IP address.

How are you authenticating users? Typically you would have them log in and then store that state in their session in your app.

Paolo
+2  A: 

You'll always get an IP address, unless your web server is listening on some sort of network that is not an IP network. But the IP address won't necessarily be unique per user.

Jeff
+1  A: 

Yes, every request must have an IP address, but as stated above, some ISP's use proxies, NAT or gateways which may not give you the individual's computer.

You can easily get this IP (in c#) with:

string IP = Context.Request.ServerVariables["REMOTE_ADDR"].ToString();

or in asp/vbscript with

IP = request.servervariables("REMOTE_ADDR")

nixkuroi
+3  A: 

You can get this from Request.ServerVariables["REMOTE_ADDR"].

It doesn't hurt to be defensive. If you're worried about some horrible error condition where this isn't set, check for that case and deal with it accordingly.

There could be many reasons for this value not to be useful. You may only get the address of the last hop, like a load balancer or SSL decoder on the local network. It might be an ISP proxy, or some company NAT firewall.

On that note, some proxies may provide the IP for which they're forwarding traffic in an additional HTTP header, accessible via Request.ServerVariables["HTTP_X_FORWARDED_FOR"]. You might want to check this first, then fall back to Request.ServerVariables["REMOTE_ADDR"] or Request.UserHostAddress.

It's certainly not a bad idea to log these things for reference/auditing.

+1 and you can add the list of issues with REMOTE_ADDR that it gets a bit funky when IPV6 is being used.
AnthonyWJones