Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get User's Manager Details from Active Directory

How can I get details like the manager name and email address from the active directory manager associated with a user?

I am able to get all details of users:

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

Thanks.

like image 518
Bokambo Avatar asked May 06 '15 08:05

Bokambo


People also ask

How do I find the Active Directory Manager name?

You can get manager with something like that: DirectoryEntry dirEntry = new DirectoryEntry("LDAP://DC=company,DC=com"); DirectorySearcher search = new DirectorySearcher(dirEntry); search.

How do I get user attributes in Active Directory using PowerShell?

Use the Set-ADUser cmdlet to change Active Directory user attributes. To display the properties of a specific user, use the –Identity parameter. Identity can be a username, login (SAMAccountName), DN (Distinguished Name), SID, or GUID.


1 Answers

I use DirectorySearcher for getting data from AD. You can get manager with something like that:

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

Now you know who is manager, so you can query for data about manager.

like image 146
Kodre Avatar answered Sep 18 '22 13:09

Kodre