简体   繁体   English

如何从DirectoryEntry和DN中检索DirectoryEntry

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

I have a DirectoryEntry object representing a user. 我有一个表示用户的DirectoryEntry对象。 From the DirectoryEntry.Properties collection, I am retrieving the "manager" property, which will give me a Distinguished Name ("DN") value for the user's manager. DirectoryEntry.Properties集合中,我正在检索"manager"属性,该属性将为用户的经理提供一个专有名称(“DN”)值。

Can I retrieve a DirectoryEntry object for the manager from just these two objects? 我可以从这两个对象中检索管理器的DirectoryEntry对象吗? If so, how? 如果是这样,怎么样?

I'm envisioning something like DirectoryEntry.GetEntryFromDN(dnManager); 我正在设想像DirectoryEntry.GetEntryFromDN(dnManager); , but I cannot find a similar call. ,但我找不到类似的电话。

Just to clarify, the DirectoryEntry and DN are the only pieces of information I have. 为了澄清,DirectoryEntry和DN是我拥有的唯一信息。 I cannot instantiate a new DirectoryEntry because then I would have have to either use the default Directory and credentials or have the Directory name/port and username/password. 我无法实例化新的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();
    }
}

You can create a new DirectoryEntry instance providing the the DN as argument and then attempt to bind (by refreshing properties for example). 您可以创建一个新的DirectoryEntry实例,将DN作为参数提供,然后尝试绑定(例如,通过刷新属性)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM