C#中获取服务器IP,客户端IP以及网卡物理地址

  1 客户端ip:
  2 Request.ServerVariables.Get("Remote_Addr").ToString();
  3 客户端主机名:
  4 Request.ServerVariables.Get("Remote_Host").ToString();
  5 客户端浏览器IE:
  6 Request.Browser.Browser;
  7 客户端浏览器 版本号:
  8 Request.Browser.MajorVersion;
  9 客户端操作系统:
 10 Request.Browser.Platform;
 11 服务器ip:
 12 Request.ServerVariables.Get("Local_Addr").ToString();
 13 服务器名:
 14 Request.ServerVariables.Get("Server_Name").ToString();
 15 如果你想进一步了解ServerVariables,可以用
 16 foreach(String o in Request.ServerVariables){
 17 Response.Write(o+"="+Request.ServerVariables[o]+"<br>");
 18 }
 19 string stringMAC = "";
 20    string stringIP = "";
 21    ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration");
 22    ManagementObjectCollection MOC= MC.GetInstances();
 23 
 24    foreach(ManagementObject MO in MOC)
 25    {
 26     if ((bool)MO["IPEnabled"] == true)
 27     {
 28        stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
 29        string[] IPAddresses = (string[]) MO["IPAddress"]; //获取本地的IP地址
 30        if(IPAddresses.Length > 0)
 31        stringIP = IPAddresses[0];
 32        Response.Write(stringMAC+"/"+stringIP);
 33      }
 34    }
 35 asp.net+c#如何获取客户端网卡的MAC地址?
 36 //要引用到以下两个命名空间
 37 using System.Diagnostics;
 38 using System.Text.RegularExpressions;
 39 
 40 //获取远程客户端的网卡物理地址(MAC)
 41 public string GetMac(string IP)   //para IP is the client's IP
 42 {
 43 string dirResults="";
 44 ProcessStartInfo psi = new ProcessStartInfo();
 45 Process proc = new Process();
 46 psi.FileName = "nbtstat";
 47 psi.RedirectStandardInput = false;
 48 psi.RedirectStandardOutput = true;
 49 psi.Arguments = "-A " + IP;
 50 psi.UseShellExecute = false;
 51 proc = Process.Start(psi);
 52 dirResults = proc.StandardOutput.ReadToEnd();
 53 proc.WaitForExit();
 54 dirResults=dirResults.Replace("\r","").Replace("\n","").Replace("\t","");
 55 
 56 Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?))__MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled);
 57 Match mc=reg.Match(dirResults+"__MAC");
 58 
 59 if(mc.Success)   
 60 {
 61 return mc.Groups["key"].Value;
 62 }
 63 else
 64 {
 65 reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled);
 66 mc=reg.Match(dirResults);
 67 if(mc.Success)
 68 {
 69 return "Host not found!";
 70 }
 71 else
 72 {
 73 return "";
 74 }
 75 }
 76 }
 77 
 78 //在页面上打印出客户端的网卡物理地址(MAC)
 79 Response.Write(this.GetMac(Request.UserHostAddress.ToString()));
 80 
 81  获取cpu序列号,硬盘ID,网卡MAC地址
 82 private void GetInfo()
 83   {
 84    string cpuInfo = "";//cpu序列号
 85    ManagementClass cimobject = new ManagementClass("Win32_Processor");
 86    ManagementObjectCollection moc = cimobject.GetInstances();
 87    foreach(ManagementObject mo in moc)
 88    {
 89     cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
 90     Response.Write ("cpu序列号:"+cpuInfo.ToString ());
 91    }
 92 
 93    //获取硬盘ID
 94    String HDid;
 95    ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");
 96    ManagementObjectCollection moc1 = cimobject1.GetInstances();
 97    foreach(ManagementObject mo in moc1)
 98    {
 99     HDid = (string)mo.Properties["Model"].Value;
100     Response.Write ("硬盘序列号:"+HDid.ToString ());
101    }
102 
103 
104    //获取网卡硬件地址
105 
106    ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
107    ManagementObjectCollection moc2 = mc.GetInstances();
108    foreach(ManagementObject mo in moc2)
109    {
110     if((bool)mo["IPEnabled"] == true)
111      Response.Write("MAC address\t{0}"+mo["MacAddress"].ToString());
112     mo.Dispose();
113    }
114   }