Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
HI
I have a requirement that I have to get the IP address of the client that requested my c# Web API, for example, if a mobile call my WebAPI , then WebAPI should identify the IP address of the mobile,what i actually want is the IP address of the requested device

What I have tried:

I have tried retrieving all the properties of the request object but none helped me
Posted
Updated 5-Mar-19 0:42am
v2

You can't. It's not a "real" IP address, it's the IP address of the point of contact to the internet, which for a mobile would be up to the phone company to provide and which will probably vary as the phone moves around networks and connects and disconnects. It will also be likely to be shared by a number of different mobile devices as any one time in the same way that a broadband connection gives the IP address of the router, not the desktop or WiFi device making the request.

Why do you think you need the IP of the phone? It's unlikely to be helpful as it is purely temporary and has no "real" geolocation so you can't even use it to tell you where a user is!
 
Share this answer
 
Comments
Santhosh Kumar 5-Mar-19 3:43am    
I just said it as an example, what i actually want is the IP address of the requested device, (improved question accordingly)
OriginalGriff 5-Mar-19 4:15am    
IP addresses are not necessarily what you think they are: they aren't the IP address of the device, because that will almost certainly always be "192.168.xxx.yyy" i.e. a *local* IP address which is never transfered past the router or other point of contact with the internet because it is irrelevant and useless - you can't attach to "192.168.0.4" and expect to get my computer because you will get the local device with the same IP on your LAN / WiFi.
What you get is the IP for the router or other point of contact, and that will be assigned by the ISP and is most often not even used just by one contact point but recycled from a pool each time the router (or whatever) connects.
This may help explain it:
https://www.codeproject.com/Tips/792723/Using-IP-based-Geolocation-and-why-its-pretty-much
It won't directly help you because the situation on a server is even worse, you only have access to what the current request gioves you in terms of IP, and that may have redirects (particularly in the case of mobile devices, where the request is often forwarded to a data compression service which intercepts requests and replies and reduces the amount of data that flies onward over the mobile network).

Why do you think you need this?
If you'd taken a minute to google you could have found the answer to this very frequently asked question - you can't get the client IP. Or their MAC address in case that is your next question.
 
Share this answer
 
 
Share this answer
 
Comments
Santhosh Kumar 5-Mar-19 3:34am    
Yes, that did not work, it gave the ip address of the server where API hosted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900