• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.net获取mac(终)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

经测试成功 只能获取同网段的哦~

顺便奉送

asp.net 获取服务器的硬盘id 网卡mac地址 cpu序列号

多个IP、端口、MAC地址的正则匹配

js获取mac地址(续)

记得引用using System.Runtime.InteropServices;

 [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);

///<summary>
///得到客户端ip
///</summary>
///<returns>ip</returns>
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;
}


///<summary>
/// 利用客户端ip得到客户端mac
///</summary>
///<param name="remoteip">客户端ip</param>
///<returns>int16类型的mac</returns>
static private Int64 getremotemac(string remoteip)
{
Int32 ldest = inet_addr(remoteip);
try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest, 0, ref macinfo, ref len);
return macinfo;
}
catch (Exception err)
{
Console.WriteLine("error:{0}", err.Message);
}
return 0;
}


///<summary>
/// int64类型的mac转换成正确的客户端mac
///</summary>
///<returns>mac</returns>
private string GetClientMAC()
{
Int64 macid = getremotemac(GetClientIP());
if (macid == 0)
return "0";
string beforeMacAddr = Convert.ToString(macid, 16);
string endMacAddr = "";
string[] macArray = new string[6];
for (int i = 0; i < 6; i++)
{
macArray[i] = beforeMacAddr.Substring(i * 2, 2);
}
for (int i = 0; i < 6; i++)
{
endMacAddr += macArray[5 - i] + "-";
}
endMacAddr = endMacAddr.Substring(0, endMacAddr.Length - 1);
endMacAddr = endMacAddr.ToUpper();
return endMacAddr;
}




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap