【问题标题】:Query Active directory to get the email property of a distinguished name directly?查询活动目录直接获取专有名称的电子邮件属性?
【发布时间】:2012-08-10 08:15:27
【问题描述】:

我现在正在活动目录中进行一些查询,我们的数据库用户 ID 与活动目录用户 ID 匹配。

我将用户 ID 与域和路径一起传递以获取我需要的内容。我的努力是从传递的用户 ID 中获取经理的电子邮件地址。当我获得 manager 属性时,我返回的是专有名称。

Finding a user's manager record in Active Directory

上面的这篇文章是我的确切问题,但它是一篇旧文章,没有关于如何前进的进一步描述,并且 OP 知道接下来要使用专有名称做什么。事实是,我没有。

所以我的问题是,如何从我迄今为止存储为前缀为 LDAP:// + "MyDistinguishedName" 的字符串的可分辨名称中获取电子邮件地址属性?

 public string GetManagerEmail(string ActiveDirectoryPath, string ActiveDirectoryDomain, bool email)
    {

        DirectoryEntry entry = new DirectoryEntry(ActiveDirectoryPath);

        try
        {
            DirectorySearcher search = new DirectorySearcher(entry);

            search.Filter = "(SAMAccountName=" + workerID + ")";
            search.PropertiesToLoad.Add("cn");
            search.PropertiesToLoad.Add("givenname");  //firstname
            search.PropertiesToLoad.Add("sn");//surname
            search.PropertiesToLoad.Add("manager");
            search.PropertiesToLoad.Add("email");
            SearchResult result = search.FindOne();

            if (null == result)
            {
                return workerID;
            }
            if (email)
            {
                return (string)result.Properties["email"][0];
            }
            else
            {
                return (string)result.Properties["manager"][0];
                //return (string)result.Properties["manager"].IndexOf[];
            }
        }
        catch (Exception ex)
        {
            throw new Exception("Error. " + ex.Message);

        }
        finally
        {
            entry.Close();
        }
    }

以上是我用来获取所需数据的方法。任何意见或改进将不胜感激。

谢谢

这是我为可能感兴趣的人提供的解决方案

            string domainAndUsername = ActiveDirectoryDomain + @"\" + workerID;
        DirectoryEntry manager = new DirectoryEntry(ActiveDirectoryPath);

        try
        {
            if (manager != null)
            {
                // get e-mail of manager 
                if (manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0)
                {
                    string managersEMail = manager.Properties["mail"].Value.ToString();
                    return managersEMail;
                }
            }

            //No email available, use contract manager
            return string.Empty;

        }
        catch (Exception ex)
        {
            throw new Exception("Error. " + ex.Message);

        }
        finally
        {
            manager.Close();
        }

【问题讨论】:

  • 请在下面添加您的解决方案作为答案。

标签: c# asp.net active-directory


【解决方案1】:

获取经理的电子邮件没有“神奇”的捷径。

一旦您检索到经理的 DN(专有名称)(在名为 managerDN 的字符串变量中),您需要通过创建 DirectoryEntry 的另一个实例再次绑定到 Active Directory 以获取经理的用户信息。

试试这样的:

 .....(your other code up here)......
 else
 {
     string managerDN = result.Properties["manager"][0].ToString();

     // fully-qualified DN for manager
     string managerFQDN = "LDAP://" + managerDN;

     DirectoryEntry manager = new DirectoryEntry(managerFQDN);

     if(manager != null)
     {
        // get e-mail of manager
        if(manager.Properties["mail"] != null && 
           manager.Properties["mail"].Count > 0)
        {
           string managersEMail = manager.Properties["mail"].Value.ToString();
           return managersEMail;
        }
     }

     // we couldn't retrieve the manager's e-mail  
     return string.Empty;
}

【讨论】:

  • 谢谢 marc_s,string managerEMail 的值是“System.DirectoryServices.PropertyValueCollection”,有什么建议吗?
  • 破解了 manager.Properties["mail"].Value.ToString();感谢您的帮助 =1 并回答 XD
  • 您可以使用 if(manager.Properties.Contains("mail")) 代替 if(manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0)
  • 我也强烈推荐 manager.RefreshCache(string[]{"mail"});否则会很慢,因为所有 AD 属性都将被加载,而不仅仅是您正在寻找的“邮件”属性
  • 还将 DirectorySearcher 和 DirectoryEntry 包装在 using 语句中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
相关资源
最近更新 更多