如何在C#中获取用户的公共IP地址
我想要使用我的网站的客户的公共IP地址。下面的代码显示了LAN中的本地IP,但我想要客户端的公共IP。
//get mac addressNetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();String sMacAddress = string.Empty;foreach (NetworkInterface adapter in nics){ if (sMacAddress == String.Empty)// only return MAC Address from first card { IPInterfaceProperties properties = adapter.GetIPProperties(); sMacAddress = adapter.GetPhysicalAddress().ToString(); }}// To Get IP Addressstring IPHost = Dns.GetHostName();string IP = Dns.GetHostByName(IPHost).AddressList[0].ToString();
输出:
IP地址:192.168.1.7
请帮我获取公共IP地址。