c# - 如何从 DirectoryEntry 和 DN 检索 DirectoryEntry

标签 c# .net active-directory ldap

我有一个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();
    }
}

关于c# - 如何从 DirectoryEntry 和 DN 检索 DirectoryEntry,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5572776/

相关文章:

c# - 为什么必须显式实现此接口(interface)?

.net - == 与 .NET 中的 Object.Equals(object)

c++ - 是否有任何 Windows 函数可以使用域名获取所有 OU(组织单位)名称?

c# - 单个 PrincipalContext 能否在应用程序的生命周期内重复使用?

c# - 如何自动将从 FieldInfo.GetValue 获得的值转换为正确的类型?

c# - 对 "complex"应用程序服务进行单元测试的正确方法

c# - 获取两个字符串的公共(public)前缀

.net - .Net 中的低延迟串行通信

C# 将 Active Directory 十六进制转换为 GUID

c# - 这两个创建实例的底层调用有什么区别?