关于从客户端获取IP地址和网卡地址的方法
关于从客户端获取IP地址和网卡地址的方法
一、其实asp.net里面有一个方法就是Page.Request.UserHostAddress,这个方法如果在局域网里还行,但是如果在广域网就不行,因为他获取的是公共的IP,那么如何获取呢?
曾经我在网上还看到一种获取MAC的方法,如下:
二、用ActionX控件获取
用控件是完全可以获取的,但是也有他的短处,就是如果客户段禁用了ActionX控件的运行,那就没希望了,这个问题我一直没解决,如果有人解决了,希望能告诉我,谢谢!
代码如下:
记住,一定要让浏览器可以运行ActionX控件。
一、其实asp.net里面有一个方法就是Page.Request.UserHostAddress,这个方法如果在局域网里还行,但是如果在广域网就不行,因为他获取的是公共的IP,那么如何获取呢?
曾经我在网上还看到一种获取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);
public string GetMAC()
{
string mac_dest = "";
try
{
string userip = Request.UserHostAddress;
Int32 ldest = inet_addr(userip); //目的地的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")
// userinfo.Text = "正在访问Localhost!";
//else
// userinfo.Text = "欢迎来自IP为" + userip + "的朋友!";
return "";
}
while (mac_src.Length < 12)
{
mac_src = mac_src.Insert(0, "0");
}
for (int i = 0; i < 11; i++)
{
if (0 == (i % 2))
mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
}
//userinfo.Text = "欢迎来自IP为" + userip + ",MAC地址为" + mac_dest + "的朋友!";
}
catch (Exception err)
{
Response.Write(err.Message);
}
return mac_dest;
}
public string GetOS()
{
HttpBrowserCapabilities info = Request.Browser;
return info.Platform.ToString();
}
public string GetIE()
{
HttpBrowserCapabilities info = Request.Browser;
return info.Browser+" "+info .Version;
}
但是在广域网里好象也不能获取到客户的MAC地址。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);
public string GetMAC()
{
string mac_dest = "";
try
{
string userip = Request.UserHostAddress;
Int32 ldest = inet_addr(userip); //目的地的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")
// userinfo.Text = "正在访问Localhost!";
//else
// userinfo.Text = "欢迎来自IP为" + userip + "的朋友!";
return "";
}
while (mac_src.Length < 12)
{
mac_src = mac_src.Insert(0, "0");
}
for (int i = 0; i < 11; i++)
{
if (0 == (i % 2))
mac_dest = mac_dest.Insert(0, mac_src.Substring(i, 2));
}
//userinfo.Text = "欢迎来自IP为" + userip + ",MAC地址为" + mac_dest + "的朋友!";
}
catch (Exception err)
{
Response.Write(err.Message);
}
return mac_dest;
}
public string GetOS()
{
HttpBrowserCapabilities info = Request.Browser;
return info.Platform.ToString();
}
public string GetIE()
{
HttpBrowserCapabilities info = Request.Browser;
return info.Browser+" "+info .Version;
}
二、用ActionX控件获取
用控件是完全可以获取的,但是也有他的短处,就是如果客户段禁用了ActionX控件的运行,那就没希望了,这个问题我一直没解决,如果有人解决了,希望能告诉我,谢谢!
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="index.aspx.cs" Inherits="manager_Index" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>title>
<link href="css/SITE.CSS" type="text/css" rel="stylesheet" />
<SCRIPT language=javascript 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=javascript 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>
</head>
<body style="text-align: center" onload="javascript:form1.TextBox1.focus();">
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 height=1></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223 height=1></OBJECT>
<SCRIPT language=javascript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</SCRIPT>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0" style="width: 600px; height: 363px;">
<tr>
<td style="width: 600px; height: 66px;">
</td>
</tr>
<tr>
<td style="width: 600px">
<img src="images/logo.jpg" /></td>
</tr>
<tr>
<td style="width: 600px; background-image: url(images/headerbg.gif); vertical-align: middle; height: 43px; background-color: #28629b;">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td style="font-weight: bold; font-size: 15px; width: 112px; color: #000066; text-align: right;">
用户名
<img src="images/login_user.gif" /></td>
<td style="width: 96px; text-align: center;">
<asp:TextBox ID="TextBox1" runat="server" CssClass="editbox3" Width="103px" onmouseover="this.focus()" onfocus="this.select()"></asp:TextBox></td>
<td style="font-weight: bold; font-size: 15px; width: 92px; color: #333366; text-align: right;">
密码
<img src="images/login_pass.gif" /></td>
<td style="width: 94px; text-align: center;">
<asp:TextBox ID="TextBox2" runat="server" CssClass="editbox3" Width="99px" onmouseover="this.focus()" onfocus="this.select()" TextMode="Password"></asp:TextBox></td>
<td style="width: 100px">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/login_btn.gif" OnClick="ImageButton1_Click" /></td>
</tr>
</table>
</td>
</tr>
</table>
<INPUT value="" name="txtMACAddr" style=" visibility:hidden">
<INPUT value="" name="txtIPAddr" style=" visibility:hidden">
<INPUT value="" name="txtDNSName" style=" visibility:hidden">
</form>
</body>
</html>
所要得地址就在三个隐藏的控件里。<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>title>
<link href="css/SITE.CSS" type="text/css" rel="stylesheet" />
<SCRIPT language=javascript 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=javascript 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>
</head>
<body style="text-align: center" onload="javascript:form1.TextBox1.focus();">
<OBJECT id=locator classid=CLSID:76A64158-CB41-11D1-8B02-00600806D9B6 height=1></OBJECT>
<OBJECT id=foo classid=CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223 height=1></OBJECT>
<SCRIPT language=javascript>
var service = locator.ConnectServer();
var MACAddr ;
var IPAddr ;
var DomainAddr;
var sDNSName;
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</SCRIPT>
<form id="form1" runat="server">
<table border="0" cellpadding="0" cellspacing="0" style="width: 600px; height: 363px;">
<tr>
<td style="width: 600px; height: 66px;">
</td>
</tr>
<tr>
<td style="width: 600px">
<img src="images/logo.jpg" /></td>
</tr>
<tr>
<td style="width: 600px; background-image: url(images/headerbg.gif); vertical-align: middle; height: 43px; background-color: #28629b;">
<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">
<tr>
<td style="font-weight: bold; font-size: 15px; width: 112px; color: #000066; text-align: right;">
用户名
<img src="images/login_user.gif" /></td>
<td style="width: 96px; text-align: center;">
<asp:TextBox ID="TextBox1" runat="server" CssClass="editbox3" Width="103px" onmouseover="this.focus()" onfocus="this.select()"></asp:TextBox></td>
<td style="font-weight: bold; font-size: 15px; width: 92px; color: #333366; text-align: right;">
密码
<img src="images/login_pass.gif" /></td>
<td style="width: 94px; text-align: center;">
<asp:TextBox ID="TextBox2" runat="server" CssClass="editbox3" Width="99px" onmouseover="this.focus()" onfocus="this.select()" TextMode="Password"></asp:TextBox></td>
<td style="width: 100px">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/images/login_btn.gif" OnClick="ImageButton1_Click" /></td>
</tr>
</table>
</td>
</tr>
</table>
<INPUT value="" name="txtMACAddr" style=" visibility:hidden">
<INPUT value="" name="txtIPAddr" style=" visibility:hidden">
<INPUT value="" name="txtDNSName" style=" visibility:hidden">
</form>
</body>
</html>
记住,一定要让浏览器可以运行ActionX控件。