1

Using Flex 4 / Air 2.0.3

Is it possible to get the active directory domain name associated with the logged on user?

This page (Get the current logged in OS user in Adobe Air) shows the user name can be inferred from the user directory folder name, however is there a way to get the domain name? (This is assuming the user is part of a domain!)

Assuming I am part of a domain "office" and my login is "j.jones", I want to retrieve both the "j.jones" and the "office" parts.

Thank you,

Jonsie

Community
  • 1
  • 1

2 Answers2

0

I have make this class:

public class UserDomainWindows extends EventDispatcher
{
    public static const EVENT_COMPLETE:String = "complete";

    private var process:NativeProcess;
    private var file:File;

    public function UserDomainWindows() 
    {

    }

    public function get():void {
        file = File.applicationStorageDirectory.resolvePath("_getuserdomain.cmd");
        var fs:FileStream = new FileStream();
        fs.open(file, FileMode.WRITE);

        fs.writeUTFBytes("@echo off\r\n");
        fs.writeUTFBytes("echo %username%\r\n");
        fs.writeUTFBytes("echo %userdomain%");

        fs.close();
        getCurrentOSUser();
    }

    private function getCurrentOSUser():void
    {       
        var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo(); 
        nativeProcessStartupInfo.executable = file;

        process = new NativeProcess();       
        process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
        process.start(nativeProcessStartupInfo);
    }

    private function onOutputData(event:ProgressEvent):void
    {           
        var output:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
        file.deleteFile();
        var sz:Array = output.split("\r\n");
        this.dispatchEvent(new UserDomainWindowsEvent("complete", sz[0], sz[1]));
    }
}

and this event class:

public class UserDomainWindowsEvent extends Event 
{
    public var username:String;
    public var userdomain:String;

    public function UserDomainWindowsEvent(type:String, username:String, userdomain:String, bubbles:Boolean=false, cancelable:Boolean=false) 
    { 
        super(type, bubbles, cancelable);
        this.userdomain = userdomain;
        this.username = username;
    } 

    public override function clone():Event 
    { 
        return new UserDomainWindowsEvent(type, this.username, this.userdomain, bubbles, cancelable);
    } 

    public override function toString():String 
    { 
        return formatToString("UserDomainWindowsEvent", "type", "username", "userdomain", "bubbles", "cancelable", "eventPhase"); 
    }

}

example:

    public function Main():void 
    {
        var ud:UserDomainWindows = new UserDomainWindows();
        ud.addEventListener(UserDomainWindows.EVENT_COMPLETE, onGetUserDomain);
        ud.get();
    }

    private function onGetUserDomain(e:UserDomainWindowsEvent):void {
        trace("username: " + e.username);
        trace("userdomain: " + e.userdomain);

    }
Alex
  • 163
  • 11
0

AIR 2.0 is able to run native processes and read their standard output. So, if your machine has AD console tools installed, you could run dsget user from AIR and read result. Also, you could take tools with your program (I have had creating AIR distribs bundled with java runtime).

alxx
  • 9,897
  • 4
  • 26
  • 41
  • Sounds great, but I'd hate to rely on these being installed. But gives me a great idea - if I read the output of "echo %USERDOMAIN%" I can get this value without external deps.... Any idea of the equivalent on 'nix / mac? – jobbie jones Aug 17 '10 at 13:08
  • uname or hostname? I'm not that strong in linux script, but there should be something. – alxx Aug 17 '10 at 13:16
  • Both would be great, although domain name ideally, as, worst case the username can be inferred from the user directory folder name. Either way I feel this approach is the winner! – jobbie jones Aug 17 '10 at 13:21
  • @jobbie jones. `echo -n $HOSTNAME` and `echo -n $USERNAME` work fine in bash under cygwin. Not sure if this works in every case, though. I'm not a *nix guy myself. – Juan Pablo Califano Aug 17 '10 at 13:38
  • Hereos. Thank you all for the input! Gotta love SO – jobbie jones Aug 17 '10 at 14:34