Monday, February 28, 2011

Another way of reading in Active Directory from within Dynamics Ax

Some posts ago we talked about reading in the Active Directory from Ax.  We used some CLR for that.  But as it is so many times with Ax, Ax has it's own routines readily available to do the job as well.  Let's look at the class xAxaptaUserManager and xAxaptaUserDetails.

Some examples:

How to get the SID from AD for a domain user:

static void GetSID(Args _args)
{ 
   xAxaptaUserManager xUsrMgr = new xAxaptaUserManager();
   ;
   info(xUsrMgr.getUserSid('youruserid','youraddomain'));
}

Note: Remember you can user the field networkalias from table UserInfo to do a conversion from Ax user id to the domain user id.

You can use this class for various purposes. Also for checking a password.

How to validate the system password in AD from within Ax

static void CheckPassword(Args _args)
{   
    xAxaptaUserManager  xUsrMgr = new xAxaptaUserManager();
    ;
    if(xUsrMgr.validatePassword('youruserid','youraddomain','yourpassword'))
        info('Password correct');
    else
        error('Password incorrect');
}

We could get some more information from the AD regarding the user by using class xAxaptaUserDetails.

How to get the user name from AD for a user

static void GetADUserName(Args _args)
{   
    xAxaptaUserManager  xUsrMgr = new xAxaptaUserManager();
    xAxaptaUserDetails  xUsrDet;
    ;
    xUsrDet = xUsrMgr.getDomainUser('youraddomain','youruserid');
    
    if(xUsrDet)
        info(xUsrDet.getUserName(0));
}

How to get the email address from AD for a user

static void GetADEmailAddress(Args _args)
{   
    xAxaptaUserManager  xUsrMgr = new xAxaptaUserManager();
    xAxaptaUserDetails  xUsrDet;
    ;
    xUsrDet = xUsrMgr.getDomainUser('youraddomain','youruserid');
    
    if(xUsrDet)
        info(xUsrDet.getUserMail(0));
}

So the AD holds no secrets for one that knows Ax...

1 comment:

  1. Works fine but what if I would like to read other LDAP fields? Is there a possiblilty to get AD-User details by SID?

    ReplyDelete