简体   繁体   English

查询Active Directory直接获取专有名称的email属性?

[英]Query Active directory to get the email property of a distinguished name directly?

I am doing some querying in active directory at the moment, our database user id matches that of the active directory user id. 我现在在活动目录中进行一些查询,我们的数据库用户ID与活动目录用户ID的匹配。

I am passing the user id along with the domain and the path to get what I need. 我传递用户ID以及域和路径以获得我需要的东西。 My endeavour is to get the email address of the manager from the passed user id. 我的努力是从传递的用户ID获取经理的电子邮件地址。 What I am returning when I get the manager property is the distinguished name. 当我获得manager属性时,我返回的是可分辨名称。

Finding a user's manager record in Active Directory 在Active Directory中查找用户的经理记录

This above post is my exact problem, but it's an old post and there are no further descriptives on how to move forward and the OP knew what to do next with the distinguished name. 上面的帖子是我的确切问题,但它是一个旧帖子,并且没有关于如何前进的进一步描述,OP知道下一步如何使用专有名称。 Truth is, I don't. 事实是,我没有。

So my question is, how to I get the email address property from the distinguished name which I have thus far stored as a string with a prefix of LDAP:// + "MyDistinguishedName"? 所以我的问题是,如何从我迄今为止存储的可分辨名称中获取电子邮件地址属性,该字符串的前缀为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();
        }
    }

Above is the method I use to get the data I need. 以上是我用来获取所需数据的方法。 Any input or improvements would be appreciated. 任何输入或改进将不胜感激。

Thanks 谢谢

THIS IS MY SOLUTION FOR THOSE THAT MAY BE INTERESTED 这是我可能感兴趣的解决方案

            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();
        }

There is no "magic" shortcut to getting the e-mail of a manager. 获取经理的电子邮件没有“神奇”的捷径。

Once you've retrieved the DN (distinguished name) of your manager (in a string variable called managerDN ), you need to again bind to Active Directory by creating another instance of a DirectoryEntry to grab the manager's user info. 一旦检索到管理器的DN(可分辨名称)(在名为managerDN的字符串变量中),您需要再次绑定到Active Directory,方法是创建另一个DirectoryEntry实例以获取管理器的用户信息。

Try something like this: 尝试这样的事情:

 .....(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;
}

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

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