asp.net 获取客户端mac地址

lucas54434688 2012-09-24 04:49:50
[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)
{


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>");
}
catch (Exception err)
{
Response.Write(err.Message);
}
}这个方法在winxp下可以获取。但是放在2003上就不行了。在2003上面怎么获取客户端的mac地址呢?有知道的大大吗?给个代码最好
...全文
218 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
wfoegl 2012-09-27
  • 打赏
  • 举报
回复
string macAddresses = "";

foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
macAddresses += nic.GetPhysicalAddress().ToString();
break;
}
}

Page.Response.Write(macAddresses);
a21999 2012-09-27
  • 打赏
  • 举报
回复
无法完成的任务,因为这是不安全的行为,浏览器会提示或者禁止该行为。
lucas54434688 2012-09-27
  • 打赏
  • 举报
回复
都没有什么切实可以行的办法吗?
lucas54434688 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

看下这个,也许有帮助。
http://stackoverflow.com/questions/10342086/what-is-aspnet-guid
// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContextBase context)
……
[/Quote]
谢谢你这么热心。。。这个是个思路!!分就给你吧!
wfoegl 2012-09-27
  • 打赏
  • 举报
回复
看下这个,也许有帮助。
http://stackoverflow.com/questions/10342086/what-is-aspnet-guid
// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContextBase context)
{
if (context.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
{
context.Session[CartSessionKey] =
context.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
context.Session[CartSessionKey] = tempCartId.ToString();
}
}
return context.Session[CartSessionKey].ToString();
}
hia0571 2012-09-27
  • 打赏
  • 举报
回复
用百度搜索 ASP.NET探针

yonglaixiazaide 2012-09-27
  • 打赏
  • 举报
回复
这个问题又出现了,个人 觉得 ,处于安全性,还有其他原因,这个是不可能的
lucas54434688 2012-09-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

生成GUID作为会话ID,然后将其分配给用户。你觉得可行不
[/Quote]
具体方法呢?大概说说怎么弄?
wfoegl 2012-09-27
  • 打赏
  • 举报
回复
生成GUID作为会话ID,然后将其分配给用户。你觉得可行不
lucas54434688 2012-09-27
  • 打赏
  • 举报
回复
我插。。。这些方法使劲贴。谢谢你的回答。。但是你实践过没啊?这些获取客户端mac不行的啊!!!既然获取mac困难。那有什么方法可以确定一台计算机的唯一性?
wfoegl 2012-09-24
  • 打赏
  • 举报
回复
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="vs_targetSchema" content="http://schemas.
microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">
<!--

function Button1_onclick() {
var locator = new ActiveXObject
"WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
var e = new Enumerator (properties);
document.write("<table border=1>");
dispHeading();
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("<tr>");
document.write("<td>" + p.Caption + "</td>");
document.write("<td>" + p.IPFilterSecurityEnabled + "</td>");
document.write("<td>" + p.IPPortSecurityEnabled + "</td>");
document.write("<td>" + p.IPXAddress + "</td>");
document.write("<td>" + p.IPXEnabled + "</td>");
document.write("<td>" + p.IPXNetworkNumber + "</td>");
document.write("<td>" + p.MACAddress + "</td>");
document.write("<td>" + p.WINSPrimaryServer + "</td>");
document.write("<td>" + p.WINSSecondaryServer + "</td>");
document.write("</tr>");
}
document.write("</table>");
}

function dispHeading()
{
document.write("<thead>");
document.write("<td>Caption</td>");
document.write("<td>IPFilterSecurityEnabled</td>");
document.write("<td>IPPortSecurityEnabled</td>");
document.write("<td>IPXAddress</td>");
document.write("<td>IPXEnabled</td>");
document.write("<td>IPXNetworkNumber</td>");
document.write("<td>MACAddress</td>");
document.write("<td>WINSPrimaryServer</td>");
document.write("<td>WINSSecondaryServer</td>");
document.write("</thead>");
}

//-->
</script>
</head>
<body>
<INPUT id="Button1" type="button" value="Button"
name="Button1" language="javascript" onclick="return Button1_onclick()">
</body>
</html>
wfoegl 2012-09-24
  • 打赏
  • 举报
回复
protected void Page_Load(object sender, EventArgs e)
{

NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

//mac
Label1.Text = nics[0].GetPhysicalAddress().ToString();
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧