2016-02-18 2 views
0

Я использую фрагмент кода ниже от System.DirectoryServices.AccountManagement для поиска пользователя в ActiveDirectory.C# access msExchRecipientTypeDetails свойство System.DirectoryServices.AccountManagement

user.Name возвращает успешно, но как я могу получить другие свойства от AD для пользователя, например msExchRecipientTypeDetails, так как он не отображается в VisualStudio 2015?

using (PrincipalContext adPrincipalContext = new PrincipalContext(ContextType.Domain, DOMAIN, USERNAME, PASSWORD)) 
{ 
    UserPrincipal userPrincipal = new UserPrincipal(adPrincipalContext);     
    userPrincipal.SamAccountName = "user-ID"; 
    PrincipalSearcher search = new PrincipalSearcher(userPrincipal); 

    foreach (var user in search.FindAll()) 
    { 
     Console.WriteLine("hei " + user.Name);   
     // how to retrive other properties from AD like msExchRecipientTypeDetails??   
    } 
} 

ответ

3

Для любых пользовательских атрибутов вам необходимо использовать DirectoryEntry. Добавьте ссылку в свой проект в «System.DirectoryServices», если вы еще этого не сделали. Так как у вас уже есть Principal объект, вы можете сделать это, чтобы получить DirectoryEntry:

var de = user.GetUnderlyingObject(); 

И потому msExchRecipientTypeDetails странное AD большое целое число, вы должны прыгать через обручи, чтобы получить реальную стоимость. Вот решение от another question для получения значения:

var adsLargeInteger = de.Properties["msExchRecipientTypeDetails"].Value; 
var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null); 
var lowPart = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null); 
var recipientType = highPart * ((Int64)UInt32.MaxValue + 1) + lowPart; 
+0

спасибо! это точно правильный ответ. –