簡體   English   中英

如何從DirectoryEntry和DN中檢索DirectoryEntry

[英]How to retrieve DirectoryEntry from a DirectoryEntry and a DN

我有一個表示用戶的DirectoryEntry對象。 DirectoryEntry.Properties集合中,我正在檢索"manager"屬性,該屬性將為用戶的經理提供一個專有名稱(“DN”)值。

我可以從這兩個對象中檢索管理器的DirectoryEntry對象嗎? 如果是這樣,怎么樣?

我正在設想像DirectoryEntry.GetEntryFromDN(dnManager); ,但我找不到類似的電話。

為了澄清,DirectoryEntry和DN是我擁有的唯一信息。 我無法實例化新的DirectoryEntry因為那時我必須使用默認目錄和憑據,或者具有目錄名稱/端口和用戶名/密碼。

DirectoryEntry User = YourPreExistingUser();

string managerDN = User.Properties["manager"][0].ToString();

// Browse up the object hierarchy using DirectoryEntry.Parent looking for the
// domain root (domainDNS) object starting from the existing user.
DirectoryEntry DomainRoot = User;

do
{
    DomainRoot = DomainRoot.Parent;
}
while (DomainRoot.SchemaClassName != "domainDNS");

// Use the domain root object we found as the search root for a DirectorySearcher
// and search for the manager's distinguished name.
using (DirectorySearcher Search = new DirectorySearcher())
{
    Search.SearchRoot = DomainRoot;

    Search.Filter = "(&(distinguishedName=" + managerDN + "))";

    SearchResult Result = Search.FindOne();

    if (Result != null)
    {
        DirectoryEntry Manager = Result.GetDirectoryEntry();
    }
}

您可以創建一個新的DirectoryEntry實例,將DN作為參數提供,然后嘗試綁定(例如,通過刷新屬性)。

DirectoryEntry e = new DirectoryEntry(dn,“u”,“p”);
e.RefreshCache();

暫無
暫無

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

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