ASP.NET獲取客戶端IP及MAC地址
朋友最近問如何獲取客戶端IP及MAC地址,一直想把這段給整理一下,契機來了;下邊分為了C#後臺獲取的方法和前臺Javascript(呼叫ActiveX)獲取的方法,大家如果有好的方法一起討論撒O(∩_∩)O
C#程式碼獲取:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
namespace MyTest
{
public partial class mac : System.Web.UI.Page
{
[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);
protected void Page_Load(object sender, EventArgs e)
{
GetMac();
}
//獲取客戶端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地址
private void GetMac()
{
// 在此處放置使用者程式碼以初始化頁面
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 + "的朋友!" + "
");
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 + "
" + ",MAC地址為" + mac_dest + "的朋友!"
+ "
");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}
}
}
Javascript獲取:
<HTML><HEAD><TITLE>WMI Scripting HTMLTITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<SCRIPT language=JScript event="OnCompleted(hResult,pErrorObject, pAsyncContext)" for=foo>
document.forms[0].txtMACAddr.value=unescape(MACAddr);
document.forms[0].txtIPAddr.value=unescape(IPAddr);
document.forms[0].txtDNSName.value=unescape(sDNSName);
//document.formbar.submit();
SCRIPT>
<SCRIPT language=JScript event=OnObjectReady(objObject,objAsyncContext) for=foo>
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true)
{
if(objObject.MACAddress != null && objObject.MACAddress != "undefined")
MACAddr = objObject.MACAddress;
if(objObject.IPEnabled && objObject.IPAddress(0) != null && objObject.IPAddress(0) != "undefined")
IPAddr = objObject.IPAddress(0);
if(objObject.DNSHostName != null && objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
SCRIPT>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR>HEAD>
<BODY>
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 VIEWASTEXT>OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223>OBJECT>
<SCRIPT language=JScript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
SCRIPT>
<FORM id=formfoo name=formbar action=NICPost.asp method=post><INPUT value=00:05:5D:0E:C7:FA name=txtMACAddr> <INPUT value=192.168.0.2 name=txtIPAddr> <INPUT value=typ name=txtDNSName> FORM>BODY>HTML>
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22392018/viewspace-766604/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 獲取客戶端Mac地址客戶端Mac
- JSF/JAVA 根據IP獲取客戶端Mac地址JSJava客戶端Mac
- java獲取客戶端ip和macJava客戶端Mac
- Spring 客戶端 IP 地址獲取及儲存細節Spring客戶端
- asp.net 獲取客戶端瀏覽器訪問的IP地址ASP.NET客戶端瀏覽器
- 服務端如何獲取客戶端請求IP地址服務端客戶端
- JS獲取客戶端IP地址與機器名JS客戶端
- 在SelfHost專案中獲取客戶端IP地址客戶端
- pomelo獲取客戶端IP客戶端
- 獲取SQL Server中連線的客戶端IP地址SQLServer客戶端
- 在OwinSelfHost專案中獲取客戶端IP地址客戶端
- c# 獲取客戶端IPC#客戶端
- 獲取客戶端真實IP客戶端
- .net 獲取客戶端真實ip客戶端
- java 獲取客戶端真實ipJava客戶端
- Django透過request獲取客戶端IPDjango客戶端
- spring boot 獲取客戶端ip資訊Spring Boot客戶端
- 在Intranet中獲得客戶端IP地址客戶端
- ASPNET獲取IP地址 MAC地址Mac
- java獲取ip地址和mac地址JavaMac
- javascript獲取客戶端ip地址省市和運營商程式碼例項JavaScript客戶端
- 一次獲取客戶端 IP 記錄客戶端
- 伺服器獲取真實客戶端 IP伺服器客戶端
- java web 通過request獲取客戶端IPJavaWeb客戶端
- 【知識積累】伺服器端獲取客戶端的IP地址(當客戶端呼叫由Axis開發的WebService)伺服器客戶端Web
- Asp.net MVC獲取訪問系統的客戶端計算機的主機名和IP地址ASP.NETMVC客戶端計算機
- Java 中獲取MAC地址 和IP地址JavaMac
- Java面試題-如何獲取客戶端真實IPJava面試題客戶端
- oracle取客戶端網路卡地址Oracle客戶端
- C# 獲取客戶端資訊 /asp.net/WebService/WebFormC#客戶端ASP.NETWebORM
- Nginx 反向代理後如何獲取真實客戶端 IPNginx客戶端
- Silverlight中利用WCF獲取客戶端IP客戶端
- 監控database上的客戶端ip地址Database客戶端
- Java/JSP獲得客戶端網路卡MAC地址的三種方法解析(附:獲得真實IP)JavaJS客戶端Mac
- ORACLE 中記錄客戶端MAC地址Oracle客戶端Mac
- 阿里雲CDN + nginx多級代理獲取客戶端IP阿里Nginx客戶端
- nginx反向代理獲取客戶端的真實IP和域名Nginx客戶端
- nginx多級代理下如何獲取客戶端真實IPNginx客戶端