mahdiahmadirad mahdiahmadirad - 2 months ago 10
ASP.NET (C#) Question

How to get a user's client IP address in ASP.NET?

We have

Request.UserHostAddress
to get the IP address in ASP.NET, but this is usually the user's ISP's IP address, not exactly the user's machine IP address who for example clicked a link. How can I get the real IP Address?

For example, in a Stack Overflow user profile it is: "Last account activity: 4 hours ago from 86.123.127.8", but my machine IP address is a bit different. How does Stack Overflow get this address?

In some web systems there is an IP address check for some purposes. For example, with a certain IP address, for every 24 hours can the user just have only 5 clicks on download links? This IP address should be unique, not for an ISP that has a huge range of clients or Internet users.

Did I understand well?

Answer

As others have said you can't do what you are asking. If you describe the problem you are trying to solve maybe someone can help? E.g. are you trying to uniquely identify your users? Could you use a cookie, or the session ID perhaps instead of the IP address?

Edit The address you see on the server shouldn't be the ISP's address, as you say that would be a huge range. The address for a home user on broadband will be the address at their router, so every device inside the house will appear on the outside to be the same, but the router uses NAT to ensure that traffic is routed to each device correctly. For users accessing from an office environment the address may well be the same for all users. Sites that use IP address for ID run the risk of getting it very wrong - the examples you give are good ones and they often fail. For example my office is in the UK, the breakout point (where I "appear" to be on the internet) is in another country where our main IT facility is, so from my office my IP address appears to be not in the UK. For this reason I can't access UK only web content, such as the BBC iPlayer (and my employer is probably very happy about this:-) ). AT any given time there would be hundreds, or even thousands of people at my company who appear to be accessing the web from the same IP address.

When you are writing server code you can never be sure what the IP address you see is refering to.In fact some users like it this way. Some people deliberately use an annoymising proxy to further confound you, for security or privacy reasons they route their web traffic via a proxy so that you won't know who or where that user is.

When you say your machine address is different to the IP address shown on StackOverflow, how are you finding out your machine address? If you are just looking locally using ipconfig or something like that I would expect it to be different for the reasons I outlined above. If you want to double check what the outside world thinks have a look at whatismyipaddress.com/.

This Wikipedia link on NAT will provide you some background on this.

Comments