【问题标题】:Get User's Manager Details from Active Directory从 Active Directory 获取用户的经理详细信息
【发布时间】:2019-10-14 17:58:11
【问题描述】:

如何从与用户关联的活动目录管理器中获取管理器名称和电子邮件地址等详细信息?

我能够获取用户的所有详细信息:

ActiveDirectory.SearchUserinAD("ads", "sgupt257");

 public static bool SearchUserinAD(string domain, string username)
        {
            using (var domainContext = new PrincipalContext(ContextType.Domain, domain))
            {
                using (var user = new UserPrincipal(domainContext))
                {
                    user.SamAccountName = username;
                    using (var pS = new PrincipalSearcher())
                    {
                        pS.QueryFilter = user;
                        var results = pS.FindAll().Cast<UserPrincipal>();
                        {
                            foreach (var item in results)
                            {                                
                                File.WriteAllText("F:\\webapps\\CIS\\UserInfo.txt", item.DisplayName + item.Name + item.EmailAddress + item.EmployeeId + item.VoiceTelephoneNumber + item.Guid + item.Context.UserName + item.Sid);
                            }
                            if (results != null && results.Count() > 0)
                            { 
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }

谢谢。

【问题讨论】:

  • 广告经理?这是什么?
  • 与用户关联的 AD Manager...
  • 从未听说过。这很奇怪,因为我在 AD 工作了大约 2 年

标签: c# active-directory


【解决方案1】:

我使用 DirectorySearcher 从 AD 获取数据。 您可以通过类似的方式获得经理:

DirectoryEntry dirEntry = new DirectoryEntry("LDAP://DC=company,DC=com");
DirectorySearcher search = new DirectorySearcher(dirEntry);
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("manager");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("sAMAccountName");
if (username.IndexOf('@') > -1)
{
    // userprincipal username
    search.Filter = "(userPrincipalName=" + username + ")";
}
else
{
    // samaccountname username
    String samaccount = username;
    if (username.IndexOf(@"\") > -1)
    {
        samaccount = username.Substring(username.IndexOf(@"\") + 1);
    }
    search.Filter = "(sAMAccountName=" + samaccount + ")";
}
SearchResult result = search.FindOne();
result.Properties["manager"][0];

现在您知道谁是经理,因此您可以查询有关经理的数据。

【讨论】:

    【解决方案2】:

    如果您想使用 Principals 而不是 DirectorySearcher,您可以在 UserPrincipal 对象上调用 GetUnderlyingObject() 并为其获取 DirectoryEntry。

    using(var user = new UserPrincipal(domainContext))
    {
        DirectoryEntry dEntry = (DirectoryEntry)user.GetUnderlyingObject();
        Object manager = dEntry.Properties["manager"][0];
    }
    

    【讨论】:

    • 使用此方法时,我收到一条错误消息,指出“必须保留 Principal 对象才能调用此方法。”获取底层对象时。有什么想法吗?
    • @DaRoGa 我相信您可能已经找到了答案,但请务必在初始化 PrincipalContext 类时指定您的域。我还使用了UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username) 而不是new UserPrincipal(domainContext)。这对我有用,因为在玩弄了上面的代码之后,我收到了一个不同的错误。
    【解决方案3】:

    我使用 DirectorySearcher 和 PrincipalSearcher 组合来返回唯一标识符 sAMAccountName 有了这个我可以从 AD 获取所有信息

    public string GetManagerId(string id)
        {
            string managerNetId = "Not_Found";
            try
            {
                using (DirectorySearcher searcher = new DirectorySearcher(Context.LdapConnection))
                {
                    //We search known user Id 
                    searcher.Filter = "(sAMAccountName=" + id + ")";
    
                    //We search Manager Property
                    searcher.PropertiesToLoad.Add("manager");
    
                    SearchResult result = searcher.FindOne();
                    string DistingedName = result.Properties["manager"][0].ToString();
    
                    // We create domain context                    
                    PrincipalContext PrContext = new PrincipalContext(ContextType.Domain, "YourDomain.com", "OU=Users,OU=****,OU=****,OU=****,DC=*****,DC=*****");
    
                    //We  define a "query-by-example" principal - here, we search for a UserPrincipal 
                    UserPrincipal qbeUser = new UserPrincipal(PrContext);
    
                    // We define parameter for search operation
                    string mngt = DistingedName.Trim();
    
                    qbeUser.Surname = mngt.Substring(mngt.IndexOf("=") + 1, mngt.IndexOf(",") - 4).ToLower();
                    string fnm = mngt.Insert(1, "\\,");
                    qbeUser.GivenName = fnm.Substring(mngt.IndexOf(",") + 4, mngt.IndexOf(",") - 5).ToLower() + "*";          
    
                    // create your principal searcher passing in the QBE principal    
                    PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
    
                    // find all matches
                    foreach (var found in srch.FindAll())
                    {
                        // We check if is realy user Manager
                        if (found.DistinguishedName == DistingedName)
                        {
                            managerNetId = found.SamAccountName;
                        }
                    }
    
                    return managerNetId;
                }
            }
            catch (Exception ex)
            {
    
                Console.WriteLine(ex.Message);
                return null;
            }
        } 
    
     public string GetManagerMail(string managerNetId)
        {
            try
            {
                using (DirectorySearcher searcher = new DirectorySearcher(Context.LdapConnection))
                {
                    searcher.Filter = "(sAMAccountName=" + id + ")";
                    searcher.PropertiesToLoad.Add("mail");
                    SearchResult result = searcher.FindOne();
                    return result.Properties["mail"][0].ToString();
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多