在Global.asax中获取客户端MAC地址

时间:2013-12-15 09:27:58

标签: javascript asp.net

我有一种使用ActiveX获取客户端MAC地址和其他信息的方法(代码如下所示)。但我只想在Global.asax页面的Session_OnStart()事件中使用MAC地址值并写入文本文件。那我怎么能这样做呢?

 <!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>
    <title></title>
        <script id="clientEventHandlersJS" type="text/javascript">

            function btnGo_onClick() {

                var macAddress;
                // Connect to WMI
                var locator = new ActiveXObject("WbemScripting.SWbemLocator");
                var service = locator.ConnectServer(".");

                // Get the info
                var properties = service.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration");
                var e = new Enumerator(properties);

                // Output info
                document.write("<table border=1>");
                document.write("<thead>");
                document.write("<td>Caption</td>");
                document.write("<td>MAC Address</td>");
                document.write("</thead>");

                for (; !e.atEnd(); e.moveNext()) {
                    var p = e.item();
                    document.write("<tr>");
                    document.write("<td>" + p.Caption + "</td>");
                    document.write("<td>" + p.MACAddress + "</td>");
                    document.write("</tr>");
                }
                document.write("</table>");
            }
        </script>
        </head>
<body>
        <h1>MAC Address</h1>
        <input id="btnGo" name="btnGo" value="Go" onclick="javascript:btnGo_onClick()" type="button">
</body>

1 个答案:

答案 0 :(得分:1)

我们的想法是将您的客户端数据发送回服务器。你可以在拥有它时使用ajax调用,或者只是创建一个img文件名,通过我在此演示的参数。

在服务器端创建处理程序,例如logmac.ashx

并在您的代码中添加此javascript脚本部分:

document.write('<img src="logmac.ashx?cap=' + escape(p.Caption) + '&mac=' + escape(p.MACAddress) +'" width="1" height="1">');

或者只是将其呈现在页面的某个位置,以便渲染类似

的行
<img src="logmac.ashx?cap=99283&mac=22:22:22:22" width="1" height="1">

甚至可以更好地制作出Royi在评论中建议的动态图像

<script>
  var img = new Image();
  img.src = 'logmac.ashx?cap=' + escape(p.Caption) + '&mac=' + escape(p.MACAddress) ;
</script>

此图像调用将调用您的处理程序,并在那里保存此信息。你的处理程序就像

// 1x1 transparent GIF
private readonly byte[] GifData = {
    0x47, 0x49, 0x46, 0x38, 0x39, 0x61,
    0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
    0x00, 0xff, 0xff, 0xff, 0x00, 0x00,
    0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
    0x01, 0x00, 0x01, 0x00, 0x00, 0x02,
    0x02, 0x44, 0x01, 0x00, 0x3b
};

public void ProcessRequest (HttpContext context) 
{
    // read here the url parameters, and save them
    context.Request["cap"].ToString();
    context.Request["mac"].ToString();

    // send the image
    context.Response.ContentType = "image/gif";
    context.Response.Buffer = false;
    context.Response.OutputStream.Write(GifData, 0, GifData.Length);
}

这个想法类似于那个:ASP server stats for html pages

如果失败,那么下一个解决方案是进行ajax调用,或以编程方式更改图像元素上的src。

相关问题