查询Active Directory,以获得直接专有名称的邮件属性?属性、直接、名称、邮件

2023-09-08 12:17:56 作者:老板,来一斤小凯

我做在Active Directory中某些查询的那一刻,我们的数据库中的用户ID匹配的Active Directory用户的ID。

我传递的用户ID和域名,并获得我所需要的路径。我的努力是从传递的用户id来获取管理员的电子邮件地址。当我得到了经理属性是可分辨的名称是什么,我回来了。

Finding在Active Directory中用户的经理纪录

这上面的帖子是我确切的问题,但它是一个老帖子,有关于如何推进没有进一步描述统计和OP知道下一步该做什么的专有名称。事实是,我不知道。

所以我的问题是,如何将我从我迄今保存为一个字符串与LDAP的preFIX的专有名称的电子邮件地址属性:// +MyDistinguishedName

 公共字符串GetManagerEmail(字符串ActiveDirectoryPath,串ActiveDirectoryDomain,布尔电子邮件)
    {

        的DirectoryEntry条目=新的DirectoryEntry(ActiveDirectoryPath);

        尝试
        {
            DirectorySearcher从搜索=新DirectorySearcher从(输入);

            sea​​rch.Filter =(SAM帐户名=+ workerID +);
            sea​​rch.PropertiesToLoad.Add(CN);
            sea​​rch.PropertiesToLoad.Add(给定名称); //名字
            sea​​rch.PropertiesToLoad.Add(SN); //姓
            sea​​rch.PropertiesToLoad.Add(经理);
            sea​​rch.PropertiesToLoad.Add(电子邮件);
            信息搜索结果的结果= search.FindOne();

            如果(空==结果)
            {
                返回workerID;
            }
            如果(电子邮件)
            {
                返回(串)result.Properties [电子邮件] [0];
            }
            其他
            {
                返回(串)result.Properties [经理] [0];
                //返回(串)result.Properties [经理]的IndexOf []。
            }
        }
        赶上(例外前)
        {
            抛出新的异常(错误。+ ex.Message);

        }
        最后
        {
            entry.Close();
        }
    }
 

以上是我用得到我需要的数据的方法。任何输入或改进将AP preciated。

感谢

Active Directory与域服务,介绍,安装

这是我的解决方案为那些可能会感兴趣

 字符串domainAndUsername = ActiveDirectoryDomain + @\+ workerID;
        的DirectoryEntry经理=新的DirectoryEntry(ActiveDirectoryPath);

        尝试
        {
            如果(经理!= NULL)
            {
                //获取电子邮件经理
                如果(manager.Properties [电子邮件] = NULL和放大器;!&安培; manager.Properties [电子邮件]计数大于0)
                {
                    字符串managersEMail = manager.Properties [电子邮件] Value.ToString()。
                    返回managersEMail;
                }
            }

            //没有电子邮件可用,使用合同管理
            返回的String.Empty;

        }
        赶上(例外前)
        {
            抛出新的异常(错误。+ ex.Message);

        }
        最后
        {
            manager.Close();
        }
 

解决方案

有没有神奇的快捷方式,以获得经理的电子邮件。

一旦检索到你的经理的DN(专有名称)(在一个名为字符串变量 managerDN ),则需要通过创建另一个重新绑定到Active Directory实例一的DirectoryEntry 抢经理的用户信息。

尝试是这样的:

  .....(你的另一code在这里了)......
 其他
 {
     字符串managerDN = result.Properties [经理] [0]的ToString();

     //完全限定的DN经理
     字符串managerFQDN =LDAP://+ managerDN;

     的DirectoryEntry经理=新的DirectoryEntry(managerFQDN);

     如果(经理!= NULL)
     {
        //获取电子邮件经理
        如果(manager.Properties [电子邮件] = NULL和放大器;!&安培;
           manager.Properties [邮件]计数和GT。 0)
        {
           字符串managersEMail = manager.Properties [电子邮件] Value.ToString()。
           返回managersEMail;
        }
     }

     //我们无法检索经理的电子邮件
     返回的String.Empty;
}
 

I am doing some querying in active directory at the moment, our database user id matches that of the active directory user id.

I am passing the user id along with the domain and the path to get what I need. My endeavour is to get the email address of the manager from the passed user id. What I am returning when I get the manager property is the distinguished name.

Finding a user's manager record in 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. 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"?

 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.

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

 
精彩推荐
图片推荐