从Active Directory获取用户的经理详细信息

5

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

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

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 管理员?这是什么? - MajkeloDev
1
AD 管理器关联到用户... - Bokambo
从来没有听说过这个。很奇怪,因为我已经使用AD工作了大约2年。 - MajkeloDev
3个回答

8

我使用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];

现在你知道谁是经理,所以你可以查询有关经理的数据。


8
如果你想使用Principal 而非DirectorySearcher,你可以在UserPrincipal对象上调用GetUnderlyingObject()方法并获取其对应的DirectoryEntry。
using(var user = new UserPrincipal(domainContext))
{
    DirectoryEntry dEntry = (DirectoryEntry)user.GetUnderlyingObject();
    Object manager = dEntry.Properties["manager"][0];
}

使用此方法时,获取基础对象时出现错误,指出“必须在调用此方法之前持久化主体对象”。有任何想法吗? - DaRoGa
@DaRoGa 我相信你可能已经找到了答案,但是在初始化PrincipalContext类时一定要指定你的域名。我也使用了UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username)而不是new UserPrincipal(domainContext)。在对上述代码进行一些尝试后,这对我起作用了,因为我收到了一个不同的错误。 - interesting-name-here

0

我使用组合的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;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接