Getting the windows user name into your Flex application
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.
March 26, 2008 at 7:42 pm
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?