【ASP.NET】#001 获取服务器IP

时间:2022-10-05 23:17:11

客户端ip:

Request.ServerVariables.Get("Remote_Addr").ToString();

客户端主机名:

Request.ServerVariables.Get("Remote_Host").ToString();

客户端浏览器IE:

Request.Browser.Browser;

客户端浏览器 版本号:

Request.Browser.MajorVersion;

客户端操作系统:

Request.Browser.Platform;

服务器ip:

Request.ServerVariables.Get("Local_Addr").ToString();

服务器名:

Request.ServerVariables.Get("Server_Name").ToString();

如果你想进一步了解ServerVariables,可以用

foreach(String o in Request.ServerVariables){ Response.Write(o+"="+Request.ServerVariables[o]+"<br>"); } string stringMAC = ""; string stringIP = ""; ManagementClass MC = new ManagementClass ("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection MOC= MC.GetInstances(); foreach(ManagementObject MO in MOC) { if ((bool)MO["IPEnabled"] == true) { stringMAC += MO["MACAddress"].ToString(); //获取网卡的地址
string[] IPAddresses = (string[]) MO["IPAddress"]; //获取本地的IP地址
if(IPAddresses.Length > ) stringIP = IPAddresses[]; Response.Write(stringMAC+"/"+stringIP); } }

 

asp.net+c#如何获取客户端网卡的MAC地址?

//要引用到以下两个命名空间
using System.Diagnostics; using System.Text.RegularExpressions; //获取远程客户端的网卡物理地址(MAC)
public string GetMac(string IP) //para IP is the client's IP
{ string dirResults=""; ProcessStartInfo psi = new ProcessStartInfo(); Process proc = new Process(); psi.FileName = "nbtstat"; psi.RedirectStandardInput = false; psi.RedirectStandardOutput = true; psi.Arguments = "-A " + IP; psi.UseShellExecute = false; proc = Process.Start(psi); dirResults = proc.StandardOutput.ReadToEnd(); proc.WaitForExit(); dirResults=dirResults.Replace("/r","").Replace("/n","").Replace("/t",""); Regex reg=new Regex("Mac[ ]{0,}Address[ ]{0,}=[ ]{0,}(?<key>((.)*?)) __MAC",RegexOptions.IgnoreCase|RegexOptions.Compiled); Match mc=reg.Match(dirResults+"__MAC"); if(mc.Success) { return mc.Groups["key"].Value; } else { reg=new Regex("Host not found",RegexOptions.IgnoreCase|RegexOptions.Compiled); mc=reg.Match(dirResults); if(mc.Success) { return "Host not found!"; } else { return ""; } } } //在页面上打印出客户端的网卡物理地址(MAC)
Response.Write(this.GetMac(Request.UserHostAddress.ToString())); 获取cpu序列号,硬盘ID,网卡MAC地址 private void GetInfo() { string cpuInfo = "";//cpu序列号
ManagementClass cimobject = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach(ManagementObject mo in moc) { cpuInfo = mo.Properties["ProcessorId"].Value.ToString(); Response.Write ("cpu序列号:"+cpuInfo.ToString ()); } //获取硬盘ID
String HDid; ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection moc1 = cimobject1.GetInstances(); foreach(ManagementObject mo in moc1) { HDid = (string)mo.Properties["Model"].Value; Response.Write ("硬盘序列号:"+HDid.ToString ()); } //获取网卡硬件地址
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc2 = mc.GetInstances(); foreach(ManagementObject mo in moc2) { if((bool)mo["IPEnabled"] == true) Response.Write("MAC address/t{0}"+mo["MacAddress"].ToString()); mo.Dispose(); } }

 

实测 TEST

这边有获取 客户端ip ,端口,域名,以及服务端地址

var requestIP = HttpContext.Current.Request.UserHostAddress;  //请求的IP地址
var requestIPName = HttpContext.Current.Request.Url.DnsSafeHost; //可能是DNS,或者域名,不一定为IP地址
var port = HttpContext.Current.Request.Url.Port; //当前请求HTTP的端口
var serverIP = HttpContext.Current.Request.ServerVariables.Get("Local_Addr").ToString(); //获取服务端IP地址
var ip = requestIP + ":" + port + "/HT/ "+" requestIP:" + requestIP + "==>requestIPName:" + requestIPName;

测试结果:

【ASP.NET】#001 获取服务器IP