asp.net 获取IP地址的可靠方法
本文导语: 1、asp.net获取IP地址的代码 1)、获取服务器的IP地址: 代码示例: using System.Net; string myIP,myMac; System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; if ( addressList.Length>1) { myIP = addressList[0].ToString(); myMac = addre...
1、asp.net获取IP地址的代码
1)、获取服务器的IP地址:
using System.Net;
string myIP,myMac;
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length>1)
{
myIP = addressList[0].ToString();
myMac = addressList[1].ToString();
}
else
{
myIP = addressList[0].ToString();
myMac = "没有可用的连接";
}
myIP地址,便是服务器端的ip地址。
2)、获取客户端的ip地址:
string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
2、通过JS获取IP地址
js获取IP地址
MAC地址:
IP地址:
主机名:
var sMacAddr="";
var sIPAddr="";
var sDNSName="";
var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
sIPAddr = objObject.IPAddress(0);
if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
sMacAddr = objObject.MACAddress;
if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
myForm.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
myForm.hostName.value=sDNSName;