Search code examples
c#asp.netvirtualboxnat

Get IP of user in NATed virtual server


I have Windows Server in VirtualBox and NATed port 80 for that. Request.UserHostAddress returns local IP of virtual network. How should i get IP of user?


Solution

  • The problem you've got is that you can't see beyond NATing. NAT stands for Network Address Translation so in this instance, you're seeing the public IP address of the NAT device, not the client. That's just the way it works. It's possible for the client to send its local IP address but you'd need to have something on the client to do that.

    A different option is to switch from NAT to Bridged networking. Bridged means that the virtual network the VM is on is joined side-by-side with the physical network the host is on. This means they'll have IP addresses which are on the same subnet and are routable from anything else on the same network. Bridges act like switches/hubs, NATs act like routers.

    As long as your server / client don't have any NAT devices between them, the IPs should be readily visible using the standard mechanisms (eg context.Request.ServerVariables["REMOTE_ADDR"], Request.UserHostAddress, etc)