开发者

How to retrieve DirectoryEntry from a DirectoryEntry and a DN

I have a DirectoryEntry object representing a user. 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.

Can I retrieve a DirectoryEntry object for the manager from just these two objects? If so, how?

I'm envisioning something like 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. I cannot instantiate a new DirectoryEnt开发者_高级运维ry because then I would have have to either use the default Directory and credentials or have the Directory name/port and username/password.


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 e = new DirectoryEntry(dn, "u", "p");
e.RefreshCache();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜