Using ActiveX to get username

2019-01-28 08:04发布

问题:

I'm working with an old intranet site written in classic ASP. I'm trying to retrieve their username they logged into their machine with. Each user is logged into AD, but I can't retrieve it from the server since the intranet site does not use AD.

I was told I could use ActiveX in order to retrieve it. I did some research and I found the following code (javascript):

var wshshell = new ActiveXObject("WScript.shell");
var username = wshshell.ExpandEnvironmentalStrings("%username%");

Currently I'm using IE8 and I get an "Automation server can't create object" error on that first line.

1) Any ideas why I'm getting the error?

2) Is there a better way to be doing this given my limitations?

回答1:

If this is done client-side, then you must have the user add the site to the Trusted Sites zone and set the security level to the lowest. Line 1 should work server-side, but I don't think line 2 is right.

Try this

var net = new ActiveXObject ( "WScript.NetWork" );
var username = net.UserName;


回答2:

Basically, its impossible to retrieve client's Windows machine information using Javascript. Because its scope is upto browser only.

For doing so you need to create COM object or say an Activex object, and using ASPX page you need to deploy it on Client's system at the very first time your page is accessed from a browser.

Now, ActiveX object has a featured to interact using javascript. You have to access the COM object or the class and function of the COM, which further interact with the system classes to get the system Information. i.e logged in client's windows user information.

var net = new ActiveXObject ( "WScript.NetWork" );
var username = net.UserName;

Above code is also initializing a COM object, if it is not deployed to your client system this script won't work.