Reply to comment

End-user account info from Active Directory for Windows network login

Relevant links:

This is sample code. Add error handling and adjust to your requirements as necessary.

loAdUser = ADUserInfo()
 
? "cn:", loAdUser.cn
? "department:", loAdUser.department
? "displayname:", loAdUser.displayName
? "fax:", loAdUser.facsimileTelephoneNumber
? "emailaddr:", loAdUser.mail
? "lastname:", loAdUser.sn
? "firstName:", loAdUser.givenName
? "telephone:", loAdUser.telephoneNumber
? "dn:", loAdUser.distinguishedName
? "upn:", loAdUser.userPrincipalName
? "title:", loAdUser.title
 
? "last logon (UTC):", ConvertADoffset2UtcTime(loAdUser.lastlogon)
? "account expires (UTC):", ConvertADoffset2UtcTime(loAdUser.accountexpires)
* Retrieve ADUser info for Windows Login 
FUNCTION ADUserInfo
LPARAMETERS tcUserName, tcUserDomain
LOCAL lcUserName, lcCurrentUserName, lcCurrentUserDomain, lcUserDomain, loNameTrans, lcDN, loAdUser
#DEFINE ADS_NAME_INITTYPE_GC  3
#DEFINE ADS_NAME_TYPE_NT4 	3
#DEFINE ADS_NAME_TYPE_1779 	1
 
lcCurrentUserName = GETENV("UserName")
lcCurrentUserDomain = GETENV("UserDomain")
 
* If parameters are not supplied, use current user login info
lcUserName = EVL(tcUserName, lcCurrentUserName)
lcUserDomain = EVL(tcUserDomain, lcCurrentUserDomain)
 
* Use NameTranslate object to get DN based on Windows Login Name
loNameTrans = CREATEOBJECT("NameTranslate")					
loNameTrans.Init( ADS_NAME_INITTYPE_GC, "")
TRY
	loNameTrans.Set(ADS_NAME_TYPE_NT4, lcUserDomain + "\" + lcUserName)
	lcDN = loNameTrans.Get(ADS_NAME_TYPE_1779)
	loAdUser = GETOBJECT("LDAP://" + lcDN)
CATCH TO oExp WHEN oExp.Errorno = 1426
	loAdUser = Null
ENDTRY	
 
RETURN loAdUser
* 64-bit integer as a number 
FUNCTION ConvertADsLargeInteger(toADsLargeInteger)
RETURN toADsLargeInteger.HighPart * (2^32) + IIF(toADsLargeInteger.LowPart < 0, 2^32, 0) + toADsLargeInteger.LowPart
 
* UTC Time from AD offset.
FUNCTION ConvertADoffset2UtcTime(toADsLargeInteger)
LOCAL lnSecondsOffset
* Offset is from 1601-01-01 in 100 nanoseconds
lnSecondsOffset = INT(ConvertADsLargeInteger(toADsLargeInteger) / 10^7)
IF lnSecondsOffset > 265046774399 
	* The date will be bigger than > 9999-12-31 23:59:59
	lnSecondsOffset = NULL
ENDIF	
 
RETURN CAST(DATETIME(1601, 01, 01) + lnSecondsOffset AS datetime)

Reply

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <java>, <powershell>, <tsql>, <visualfoxpro>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.