简体   繁体   English

Active Directory-获取管理员帐户(来自专有名称)

[英]Active Directory - Get Manager account (from Distinguished Name)

I am trying to get the manager's account for a user account in active directory. 我正在尝试为Active Directory中的用户帐户获取经理帐户。

Here's the code I have.. 这是我的代码。

using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;


DirectoryContext directoryContext = new  DirectoryContext(DirectoryContextType.Domain, "MyDomain");
Domain domain = Domain.GetDomain(directoryContext);

// Find MY directory Entry
DirectorySearcher search = new DirectorySearcher(domain.GetDirectoryEntry())
{
    Filter = String.Format("(SAMAccountName={0})", "<my user id>")
};
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("manager");
DirectoryEntry userAccount = search.FindOne()?.GetDirectoryEntry();

As you can see, there's a property called manager that is requested and comes back as 如您所见,有一个名为manager的属性被请求并以

CN= Manager Name ,OU=Employee,OU=United Kingdom, OU=CompantUsers, DC=MyDomain, DC=xxx,DC=zzzzz CN = 经理姓名 ,OU =员工,OU =英国,OU = CompantUsers,DC = MyDomain,DC = xxx,DC = zzzzz

The CN= Manager Name is the full name, not the LoginID/ SAMAccountName (as used when I searched for MY AD entry ... so how can I now find the AD entry for my manager CN = 管理员名称是全名,而不是LoginID / SAMAccountName (当我搜索我的AD条目时使用的名称...因此现在如何找到我的管理员的AD条目

Ahhh ... When you know the right question to ask then Google knows the answer ... I did not know that the CN..... string was known as a distinguishedName 啊……当您知道要问的正确问题时,Google便知道了答案……我不知道CN .....字符串被称为distinguishedName

if (userAccount.Properties["manager"].Value != null)
{
  DirectorySearcher search2 = new DirectorySearcher(domain.GetDirectoryEntry())
  {
    Filter = string.Format("(distinguishedName={0})", userAccount.Properties["manager"].Value)
  };
  search2.PropertiesToLoad.Add("displayName");
  search2.PropertiesToLoad.Add("mail");
  search2.PropertiesToLoad.Add("manager");
  DirectoryEntry mgrAcc = search2.FindOne()?.GetDirectoryEntry();
}

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

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