Add reference to COM "Active DS Type Library"
Int32 nameTypeNT4 = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_NT4;
Int32 nameTypeDN = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_1779;
Int32 nameTypeUserPrincipalName = (int) ActiveDs.ADS_NAME_TYPE_ENUM.ADS_NAME_TYPE_USER_PRINCIPAL_NAME;
ActiveDs.NameTranslate nameTranslate = new ActiveDs.NameTranslate();
// Convert NT name DOMAIN\User into AD distinguished name
// "CN= User\\, Name,OU=IT,OU=All Users,DC=Company,DC=com"
nameTranslate.Set(nameTypeNT4, ntUser);
String distinguishedName = nameTranslate.Get(nameTypeDN);
Console.WriteLine(distinguishedName);
// Convert AD distinguished name "CN= User\\, Name,OU=IT,OU=All Users,DC=Company,DC=com"
// into NT name DOMAIN\User
ntUser = String.Empty;
nameTranslate.Set(nameTypeDN, distinguishedName);
ntUser = nameTranslate.Get(nameTypeNT4);
Console.WriteLine(ntUser);
// Convert NT name DOMAIN\User into AD UserPrincipalName [email protected]
nameTranslate.Set(nameTypeNT4, ntUser);
String userPrincipalName = nameTranslate.Get(nameTypeUserPrincipalName);
Console.WriteLine(userPrincipalName);