简体   繁体   中英

How to obtain local ip address

I've managed to get clients' ip addresses from my server using codes below

OperationContext context = OperationContext.Current;
MessageProperties prop = context.IncomingMessageProperties;
RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ipaddress = endpoint.Address;

However, if clients are under the same network, all the ip addresses are public ip address of the network.

Is there a way to get local ip addresses of each client if they are under the same AP? or just simply, is there a easier way to distinguish client from one another?

try this

HttpRequest currentRequest = HttpContext.Current.Request;
string ipAddress = currentRequest.ServerVariables["HTTP_X_FORWARDED_FOR"];

if (ipAddress == null || ipAddress.ToLower() == "unknown")
    ipAddress = currentRequest.ServerVariables["REMOTE_ADDR"];

return ipAddress;

由于网络地址转换,并非总是可以通过唯一的客户端地址来识别客户端,请参见如何在ASP.NET中获取用户的客户端IP地址?

您可以尝试使用webrtc的内置功能来连接到STUN服务器https://diafygi.github.io/webrtc-ips/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM