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

Javascript 正在Global.asax中获取客户端MAC地址,javascript,asp.net,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="h

我有一种使用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>

函数btnGo_onClick(){
var-macAddress;
//连接到WMI
var定位器=新的ActiveXObject(“WbemScripting.SWbemLocator”);
var service=locator.ConnectServer(“.”);
//获取信息
var properties=service.ExecQuery(“从Win32_NetworkAdapterConfiguration中选择*);
var e=新枚举数(属性);
//输出信息
文件。填写(“”);
文件。填写(“”);
文件。书写(“标题”);
文件。写入(“MAC地址”);
文件。填写(“”);
对于(;!e.atEnd();e.moveNext()){
var p=e.项目();
文件。填写(“”);
文件。写(“+p.标题+”);
document.write(“+p.MACAddress+”);
文件。填写(“”);
}
文件。填写(“”);
}
MAC地址

我们的想法是将客户端数据发送回服务器。您可以在有ajax调用时使用它,或者只创建一个img文件名来传递参数,正如我在这里演示的那样

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

并在代码中添加以下javascript脚本部分:

document.write('<img src="logmac.ashx?cap=' + escape(p.Caption) + '&mac=' + escape(p.MACAddress) +'" width="1" height="1">');
这个想法与此类似:


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

您认为可以在客户端硬盘上将此文件变白,但实际上不能。所以,也许您希望将其保存在cookie上,但这可以由用户更改…而且此脚本在javascript客户端上运行-与global.asax无关!我只想将上面代码中的MAC地址值保存到我在Global.asax中声明的变量中。可能吗?你的设计错了。你喜欢写什么MAC地址,可能是客户端-为什么在global.asax上?每一页都有调用。。。要从客户端到服务器写东西,您必须做一些技巧,比如ajax调用和发送保存在某处的信息。。。我不确定您是否理解您编写的代码的作用-如果您不理解,我不知道您如何设计代码。我想将客户端信息保存在文本文件(服务器端)中,并在Global.asax中的Session_Start()事件中执行代码。但我只能使用javascript获取客户端MAC地址。因此,我想将javascript mac地址vass传递给my Global.asax,并将用户信息另存为服务器中的文本文件。他能获得客户端mac地址吗?仅通过使用activX?那么FF(等)呢?@RoyiNamir你好,Royi。MAC地址只能在客户端上读取。如果浏览器不支持,那么您需要某种本地插件。您好:-)。(只是问)只是他的activeX得到了MAC…对吗?那么FF呢(我相信没有全球性的解决方案……?@RoyiNamir是的,acriveX得到了MAC。什么是FF?和p.s.:您可以忽略

<img src="logmac.ashx?cap=99283&mac=22:22:22:22" width="1" height="1">
<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);
}