Getting the windows user name into your Flex application
Mar/086
Got this question last week and found out you can get the windows user information into your Flex application. I’m not sure why you would wanna do this
But just for anyone trying to figure this out … here is how to do it.
Through javascript you can get the user Id (This only works through Internet Explore !), so by using ExternalInterface in Flex you can call the javascript function that will get you the user id.
So first you wanna add the javascript to your index.template.html (in html-template folder of your flex builder project). Add the script to the script tag just below the body tag.
var wshShell = new ActiveXObject(“WScript.Shell”);
function getUserName()
{
return wshShell.ExpandEnvironmentStrings(“%USERNAME%”);
}
Then you can add to your MXML application this code to get the user id …
try {
var name:String = ExternalInterface.call(‘getUserName’);
Alert.show(“name = ” + name);
} catch (error:SecurityError) {
Alert.show(“A SecurityError occurred: ” + error.message + “\n”);
} catch (error:Error) {
Alert.show(“An Error occurred: ” + error.message + “\n”);
}
} else {
Alert.show(“External interface is not available for this container.”);
}
}
So as I said … it will only work on Internet Explore. Through the wshShell object you can also get the domainname if needed.
Leave a comment
No trackbacks yet.
7:42 pm on March 26th, 2008
We are developing internal applications with Flex, these internal applications need to get the user name automatically from windows we are using IIS as the web server and we are using windows authentication on the web server is there any way to pull the user name from the web server instead using the CGI environmental variables? Is there a way to access the CGI environemtal variable using Flex?
4:18 pm on August 25th, 2008
I am getting a “Automation server can’t create object” when I run the line:
wshShell = new ActiveXObject(“WScript.Shell”);
do you know of any reason for this?
Regards,
Jordan
7:37 pm on January 4th, 2009
yes I get the same error. I altered the settings of IE.
Tools > Internet options > Security > Custom Level
Under the ActiveX controls and plug-ins, select Enable for Initializing and Script ActiveX controls not marked as safe.
8:59 pm on June 2nd, 2009
What is to prevent someone from impersonating another user by doing something such as:
C:\Documents and Settings\me>echo %USERNAME%
me
C:\Documents and Settings\me>set USERNAME=you
C:\Documents and Settings\me>echo %USERNAME%
you
9:33 pm on November 6th, 2009
@Jeremy: it’s to prevent:
var wshShell = new ActiveXObject(“WScript.Shell”);
function getUserName()
{
var WshNetwork = new ActiveXObject(“WScript.Network”);
alert(“Nombre de usuario = ” + WshNetwork.UserName);
}
Regards !
3:47 pm on November 16th, 2009
What if I have to get the username in the Flex AIR application? Will this code work?