簡體   English   中英

C#通過System.DirectoryServices.AccountManagement訪問msExchRecipientTypeDetails屬性

[英]C# access msExchRecipientTypeDetails property by System.DirectoryServices.AccountManagement

我使用下面System.DirectoryServices.AccountManagement的代碼片段在ActiveDirectory中搜索用戶。

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??          
    }
}

您需要對任何類似的自定義屬性使用DirectoryEntry。 如果尚未在項目中添加對“ System.DirectoryServices”的引用。 由於您已經有了Principal對象,因此可以執行以下操作獲取DirectoryEntry:

var de = user.GetUnderlyingObject();

而且因為msExchRecipientTypeDetails是一個奇怪的AD大整數,所以您必須跳過圈以獲得真實值。 這是獲得價值的另一個問題的解決方案:

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);
if (lowPart < 0) highPart++;
var recipientType = highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;

更新:三年后,我不得不再次查找此內容,偶然發現了自己的答案。 但事實證明,有時候我不應該得到負值。 我在這里找到答案:

解決方法是,每當LowPart方法返回的值為負數時,將HighPart方法返回的值增加一。

因此,我添加了該位: if (lowPart < 0) highPart++;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM