0

I have got a way of getting client MAC address and other information using ActiveX (the code is shown below). But I want only MAC Address value into my Session_OnStart() event in Global.asax page and write to a text file. So how can I do this?

 <!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>

Reshad
  • 49
  • 1
  • 11
  • You believe that you can white this file on clients hard disk - but actually you can not. So maybe you wish to save it on cookie, but this can be change by user... – Aristos Dec 15 '13 at 09:30
  • Also this script is run on javascript client side - nothing to do with global.asax ! – Aristos Dec 15 '13 at 09:32
  • I only want to save the MAC address value from the above code into the variable I declared in my Global.asax. Is it possible? – Reshad Dec 15 '13 at 09:43
  • Your design is wrong. What MAC address you like to write, probably client - why on global.asax ? there is called on every page call... to write something from client to server you must make some tricks, like ajax call and send the infos, saved somewhere... I am not sure you have understand what the code you have write do - and if you do not understand that, how you design your code I do not know. – Aristos Dec 15 '13 at 09:49
  • I want to save the client info in a text file(server side) and I execute my code in Session_Start() event in Global.asax.But I can only get client MAC address using javascript. So I want to pass the javascript mac address vass to my Global.asax and save as a user info to a text file in my server. – Reshad Dec 15 '13 at 09:55
  • And how you "connect" this "file" with your client browser to read it back ? Or you only try to make log ? After you read it with javascript you need to make an ajax call back to the server, send this infos, and save them. – Aristos Dec 15 '13 at 10:06
  • I only try to make log. I implement the above code in my master page. But I can't go further. Can you give me a detailed solution as answer to use the javascript mac address value in my server side and save to a text file? – Reshad Dec 15 '13 at 10:13

1 Answers1

1

The idea is to send your client side data, back to the server. You can do that with ajax call when you have it, or just create an img file name, that pass your parametres as I demonstrate here.

Create a handler on your server side , eg logmac.ashx

and add this javascript script part on your code:

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

or just render it somewhere on your page so you have render a line like

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

or even better just make a dynamical image as Royi suggest on comments

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

This image call will call your handler, and there you save this informations. You handler will be like

// 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);
}

The idea is similar to that one: ASP server stats for html pages

If this fail, then the next solution is to make ajax call, or programmatically change the src on an image element.

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150