public string GetUserIp()
{
var visitorsIpAddr = string.Empty;
if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
{
visitorsIpAddr = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
}
else if (!string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.UserHostAddress))
{
visitorsIpAddr = System.Web.HttpContext.Current.Request.UserHostAddress;
}
if (visitorsIpAddr == "::1" || visitorsIpAddr == "localhost")
{
try
{
WebRequest request = WebRequest.Create("http://checkip.dyndns.org/");
using (WebResponse response = request.GetResponse())
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
visitorsIpAddr = sr.ReadToEnd();
}
//scrape ip from the html
int i1 = visitorsIpAddr.IndexOf("Address: ", StringComparison.Ordinal) + 9;
int i2 = visitorsIpAddr.LastIndexOf("</body>", StringComparison.Ordinal);
visitorsIpAddr = visitorsIpAddr.Substring(i1, i2 - i1);
}
catch (Exception e)
{
Log.Error("Get Local IP Failed",e);
}
}
return visitorsIpAddr;
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
获取服务器电脑名: Page.Server.ManchineName
获取用户信息: Page.User
获取客户端电脑名:Page.Request.UserHostName
获取客户端电脑IP: Page.Request.UserHostAddress
1. 在ASP.NET中专用属性:
获取服务器电脑名:Page.Server.ManchineName
获取用户信息:Page.User
获取客户端电脑名:Page.Request.UserHostName
获取客户端电脑IP:Page.Request.UserHostAddress
2. 在网络编程中的通用方法:
获取当前电脑名:static System.Net.Dns.GetHostName()
根据电脑名取出全部IP地址:static System.Net.Dns.Resolve(电脑名).AddressList
也可根据IP地址取出电脑名:static System.Net.Dns.Resolve(IP地址).HostName
3. 系统环境类的通用属性:
当前电脑名:static System.Environment.MachineName
当前电脑所属网域:static System.Environment.UserDomainName
当前电脑用户:static System.Environment.UserName
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//引入相应的空间信息
using System.Text.RegularExpressions;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//此处输入Ip地址,你可以做成接受文本框的值进行查询
Response.Write(GetCustomerMac("192.168.168.168"));
}
public string GetCustomerMac(string 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();
//匹配mac地址
Match m = Regex.Match(dirResults, "\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w+\\-\\w\\w");
//若匹配成功则返回mac,否则返回找不到主机信息
if (m.ToString() != "")
return m.ToString();
else
return "找不到主机信息";
}
}
如果我们在asp.net中有需求要获取客户端计算机的名称,可以通过Request.UserHostName获取,也可以在引用System.Net命名空间后,使用下面的方法获取:
///<summary>
/// 获取客户端计算机名称
///</summary>
///<returns></returns>
publicstring GetClientComputerName()
{
string clientIP = Request.UserHostAddress;//获取客户端的IP主机地址
IPHostEntry hostEntry = Dns.GetHostEntry(clientIP);//获取IPHostEntry实体
return hostEntry.HostName;//返回客户端计算机名称
}
//获取远程客户端的浏览器信息
HttpBrowserCapabilities httpbc=System.Web.HttpContext.Current.Request.Browser;
string strInfo= "您好,您正在使用 " + httpbc.Browser + " v. " + httpbc.Version + ",你的运行平台是 " + httpbc.Platform ;
//获取远程客户端的ip主机地址
strInfo=System.Web.HttpContext.Current.Request.UserHostAddress;
//获取远程客户端的DNS名称
strInfo=System.Web.HttpContext.Current.Request.UserHostName;
//客户端上次请求的URL路径
strInfo=System.Web.HttpContext.Current.Request.UrlReferrer.ToString();
//当前请求的URl
strInfo=System.Web.HttpContext.Current.Request.Url.ToString();
//客户端浏览器的原始用户代理信息
strInfo=System.Web.HttpContext.Current.Request.UserAgent;
//获取浏览器+版本号
public string getBrowser()
{
string browsers;
HttpBrowserCapabilities bc = HttpContext.Current.Request.Browser;
string aa = bc.Browser.ToString();
string bb = bc.Version.ToString();
browsers = aa + bb;
return browsers;
}
//获取客户端IP地址
public string getIP()
{
string result = String.Empty;
result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
if (null == result || result == String.Empty)
{
return "0.0.0.0";
}
return result;
}
//获取操作系统版本号
public static string SystemCheck()
{
string Agent = HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"];
if (Agent.IndexOf("NT 4.0") > 0)
return "Windows NT ";
else if (Agent.IndexOf("NT 5.0") > 0)
return "Windows 2000";
else if (Agent.IndexOf("NT 5.1") > 0)
return "Windows XP";
else if (Agent.IndexOf("NT 5.2") > 0)
return "Windows 2003";
else if (Agent.IndexOf("NT 6.0") > 0)
return "Windows Vista";
else if (Agent.IndexOf("NT 7.0") > 0)
return "Windows 7";
else if (Agent.IndexOf("NT 8.0") > 0)
return "Windows 8";
else if (Agent.IndexOf("WindowsCE") > 0)
return "Windows CE";
else if (Agent.IndexOf("NT") > 0)
return "Windows NT ";
else if (Agent.IndexOf("9x") > 0)
return "Windows ME";
else if (Agent.IndexOf("98") > 0)
return "Windows 98";
else if (Agent.IndexOf("95") > 0)
return "Windows 95";
else if (Agent.IndexOf("Win32") > 0)
return "Win32";
else if (Agent.IndexOf("Linux") > 0)
return "Linux";
else if (Agent.IndexOf("SunOS") > 0)
return "SunOS";
else if (Agent.IndexOf("Mac") > 0)
return "Mac";
else if (Agent.IndexOf("Linux") > 0)
return "Linux";
else if (Agent.IndexOf("Windows") > 0)
return "Windows";
return "未知类型";
}
}
使用这个函数可以用ASP.NET开发服务器探针程序。
服务端信息:
Label1.Text = 服务器名称:+Server.MachineName;//服务器名称
Label2.Text = 服务器IP地址: + Request.ServerVariables[LOCAL_ADDR];//服务器IP地址
Label3.Text = 服务器域名: + Request.ServerVariables[SERVER_NAME];//服务器域名
Label4.Text = .NET解释引擎版本: + .NET CLR + Environment.Version.Major + . + Environment.Version.Minor + . + Environment.Version.Build + . + Environment.Version.Revision;//.NET解释引擎版本
Label5.Text = 服务器操作系统版本: + Environment.OSVersion.ToString();//服务器操作系统版本
Label6.Text = 服务器IIS版本: + Request.ServerVariables[SERVER_SOFTWARE];//服务器IIS版本
Label7.Text = HTTP访问端口: + Request.ServerVariables[SERVER_PORT];//HTTP访问端口
Label8.Text = 虚拟目录的绝对路径: + Request.ServerVariables[APPL_RHYSICAL_PATH];//虚拟目录的绝对路径
Label9.Text = 执行文件的绝对路径: + Request.ServerVariables[PATH_TRANSLATED];//执行文件的绝对路径
Label10.Text = 虚拟目录Session总数: + Session.Contents.Count.ToString();//虚拟目录Session总数
Label11.Text = 虚拟目录Application总数: + Application.Contents.Count.ToString();//虚拟目录Application总数
Label12.Text = 域名主机: + Request.ServerVariables[HTTP_HOST];//域名主机
Label13.Text = 服务器区域语言: + Request.ServerVariables[HTTP_ACCEPT_LANGUAGE];//服务器区域语言
Label14.Text = 用户信息: + Request.ServerVariables[HTTP_USER_AGENT];
Label14.Text=CPU个数:+Environment.GetEnvironmentVariable(NUMBER_OF_PROCESSORS);//CPU个数
Label15.Text = CPU类型: + Environment.GetEnvironmentVariable(PROCESSOR_IDENTIFIER);//CPU类型
Label16.Text = 进程开始时间: + GetPrStart();//进程开始时间
Label17.Text = AspNet 内存占用: + GetAspNetN();//AspNet 内存占用
Label18.Text = AspNet CPU时间: + GetAspNetCpu();//AspNet CPU时间
Label19.Text = FSO 文本文件读写: + Check(Scripting.FileSystemObject);//FSO 文本文件读写
Label20.Text = 应用程序占用内存 + GetServerAppN();//应用程序占用内存
客户端信息:
Request.Browser.MajorVersion.ToString();//获取客户端浏览器的(主)版本号Request.Browser.Version.ToString();//获取客户端浏览器的完整版本号
Request.Browser.Platform.ToString();//获取客户端使用平台的名字
Request.Browser.Version; //浏览器版本
HttpRequest.Url //获取有关当前请求的 URL 的信息。
HttpRequest.UrlReferrer //获取有关客户端上次请求的 URL 的信息,该请求链接到当前的 URL。
HttpRequest.UserAgent //获取客户端浏览器的原始用户代理信息。
HttpRequest.UserHostAddress //获取远程客户端的 IP 主机地址。
HttpRequest.UserHostName //获取远程客户端的 DNS 名称。
HttpRequest.UserLanguages //获取客户端语言首选项的排序字符串数组。
HttpRequest.UserAgent //获取客户端浏览器的原始用户代理信息。
HttpBrowserCapabilities bc = Request.Browser;
Response.Write(浏览器信息:);
Response.Write(浏览器= + bc.Browser + );
Response.Write(型态= + bc.Type + );
Response.Write(名称= + bc.Browser + );
Response.Write(版本= + bc.Version + );
Response.Write(使用平台= + bc.Platform + );
Response.Write(是否为测试版= + bc.Beta + );
Response.Write(是否为16 位的环境= + bc.Win16 + );
Response.Write(是否为32 位的环境= + bc.Win32 + );
Response.Write(是否支持框架(frame) = + bc.Frames + );
Response.Write(是否支持表格(table) = + bc.Tables + );
Response.Write(是否支持cookie = + bc.Cookies + );
Response.Write(是否支持vb script = + bc.VBScript + );
Response.Write(是否支持java script = + bc.JavaScript + );
Response.Write(是否支持java applets = + bc.JavaApplets + );
Response.Write(是否支持activex controls = + bc.ActiveXControls + );
ASP.NET获取客户端IP及MAC地址很多时候非常必要的,像做投票、留言、评论 等系统的时候防止别人恶意操作,导致数据的虚假行。下面介绍获取客户端的IP和MAC地址的方法,仅供参考:
获取客户端IP:
private string GetClientIP()
{
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = HttpContext.Current.Request.UserHostAddress;
}
return result;
}
获取MAC地址:
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
try
{
string userip=Request.UserHostAddress;
string strClientIP = Request.UserHostAddress.ToString().Trim();
Int32 ldest = inet_addr(strClientIP); //目的地的ip
Int32 lhost = inet_addr(""); //本地服务器的ip
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
string mac_src=macinfo.ToString("X");
if(mac_src == "0")
{
if(userip=="127.0.0.1")
Response.Write ("正在访问Localhost!");
else
Response.Write ("欢迎来自IP为" + userip + "的朋友!" + "<br>");
return;
}
while(mac_src.Length<12)
{
mac_src = mac_src.Insert(0,"0");
}
string mac_dest="";
for(int i=0;i<11;i++)
{
if (0 == (i % 2))
{
if ( i == 10 )
{
mac_dest = mac_dest.Insert(0,mac_src.Substring(i,2));
}
else
{
mac_dest ="-" + mac_dest.Insert(0,mac_src.Substring(i,2));
}
}
}
Response.Write ("欢迎来自IP为"+userip+ "<br>" + ",MAC地址为"+mac_dest+"的朋友!" + "<br>");
}
catc (Exception err)
{
Response.Write(err.Message);
}
}