Friday, March 25, 2016

Get Some AD info in C#

     private void getUsersADInfoToolStripMenuItem_Click(object sender, EventArgs e)  
     {  
       UserTools ut = new UserTools();  
       var combinedList = new List<string>();  
       combinedList.AddRange(ut.GetADUserInfo(profileNameBox.Text, fullyQualifiedDomainName, organizationalUnit));  
       combinedList.Add("");  
       combinedList.Add("GROUP MEMBERSHIPS: ");  
       combinedList.AddRange(ut.GetGroups(profileNameBox.Text, fullyQualifiedDomainName, organizationalUnit));  
       //ListBoxForm.ShowList(combinedList, "AD Info for " + ut.GetFullName(profileNameBox.Text, fullyQualifiedDomainName, organizationalUnit));  
       TextBoxForm.ShowText(string.Join<string>(Environment.NewLine, combinedList), "AD Info for " + ut.GetFullName(profileNameBox.Text, fullyQualifiedDomainName, organizationalUnit));  
     }  
     public List<string> GetADUserInfo(string user, string fQDomainName, string ou)  
     {  
       List<string> resultList = new List<string>();  
       if (!string.IsNullOrEmpty(user))  
       {  
         try  
         {  
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fQDomainName, ou);  
           UserPrincipalsEx usr = UserPrincipalsEx.FindByIdentity(ctx, user);  
           if (usr != null)  
           {  
             resultList.Add("NAME:\t\t" + usr.DisplayName);  
             resultList.Add("TITLE:\t\t" + usr.title);  
             resultList.Add("DEPARTMENT:\t" + usr.department);  
             resultList.Add("COMPANY:\t" + usr.company);  
             resultList.Add("OFFICE:\t\t" + usr.office);  
             resultList.Add("EMAIL:\t\t" + usr.EmailAddress);  
             resultList.Add("LOGIN:\t\t" + usr.SamAccountName);  
             resultList.Add("PROFILE:\t" + usr.HomeDirectory);  
             if(!string.IsNullOrEmpty(usr.Description))  
             {  
               resultList.Add("DESCRIPTION:\t" + usr.Description);  
             }  
             usr.Dispose();  
           }  
           else resultList.Add("");  
           ctx.Dispose();  
           return resultList;  
         }  
         catch (Exception unlockException)  
         {  
           MessageBox.Show(unlockException.Message);  
           resultList.Add("");  
           return resultList;  
         }  
       }  
       else resultList.Add("");  
       return resultList;  
     }  
     public string GetSamAccountName(string user, string fQDomainName, string ou)  
     {  
       string samAccountName;  
       if (!string.IsNullOrEmpty(user))  
       {  
         try  
         {  
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fQDomainName, ou);  
           UserPrincipalsEx usr = UserPrincipalsEx.FindByIdentity(ctx, user);  
           if (usr != null)  
           {  
             samAccountName = usr.SamAccountName.ToString();  
             usr.Dispose();  
           }  
           else samAccountName = "";  
           ctx.Dispose();  
           return samAccountName;  
         }  
         catch (Exception getSamAccountNameException)  
         {  
           MessageBox.Show(getSamAccountNameException.Message);  
           samAccountName = "";  
           return samAccountName;  
         }  
       }  
       else samAccountName = "";  
       return samAccountName;  
     }  
     public string GetFullName(string user, string fQDomainName, string ou)  
     {  
       string fullName;  
       if (!string.IsNullOrEmpty(user))  
       {  
         try  
         {  
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fQDomainName, ou);  
           UserPrincipalsEx usr = UserPrincipalsEx.FindByIdentity(ctx, user);  
           if (usr != null)  
           {  
             fullName = usr.Name.ToString();  
             usr.Dispose();  
           }  
           else fullName = "";  
           ctx.Dispose();  
           return fullName;  
         }  
         catch (Exception getFullNameException)  
         {  
           MessageBox.Show(getFullNameException.Message);  
           fullName = "";  
           return fullName;  
         }  
       }  
       else fullName = "";  
       return fullName;  
     }  
     public string GetEmailAddress(string user, string fQDomainName, string ou)  
     {  
       string emailAddress;  
       if (!string.IsNullOrEmpty(user))  
       {  
         try  
         {  
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fQDomainName, ou);  
           UserPrincipalsEx usr = UserPrincipalsEx.FindByIdentity(ctx, user);  
           if (usr != null)  
           {  
             emailAddress = usr.EmailAddress.ToString();  
             usr.Dispose();  
           }  
           else emailAddress = "";  
           ctx.Dispose();  
           return emailAddress;  
         }  
         catch (Exception getEmailException)  
         {  
           MessageBox.Show(getEmailException.Message);  
           emailAddress = "";  
           return emailAddress;  
         }  
       }  
       else emailAddress = "";  
       return emailAddress;  
     }  
     public string GetManager(string user, string fQDomainName, string ou)  
     {  
       string managerName;  
       if (!string.IsNullOrEmpty(user))  
       {  
         try  
         {  
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fQDomainName, ou);  
           UserPrincipalsEx usr = UserPrincipalsEx.FindByIdentity(ctx, user);  
           if (usr != null)  
           {  
             managerName = GetFullName(usr.manager, fullyQualifiedDomainName, organizationalUnit);  
             usr.Dispose();  
           }  
           else managerName = "";  
           ctx.Dispose();  
           return managerName;  
         }  
         catch (Exception getManager)  
         {  
           MessageBox.Show(getManager.Message);  
           managerName = "";  
           return managerName;  
         }  
       }  
       else managerName = "";  
       return managerName;  
     }  
     public string GetCompany(string user, string fQDomainName, string ou)  
     {  
       string company;  
       if (!string.IsNullOrEmpty(user))  
       {  
         try  
         {  
           PrincipalContext ctx = new PrincipalContext(ContextType.Domain, fQDomainName, ou);  
           UserPrincipalsEx usr = UserPrincipalsEx.FindByIdentity(ctx, user);  
           if (usr != null)  
           {  
             company = usr.company.ToString();  
             usr.Dispose();  
           }  
           else company = "";  
           ctx.Dispose();  
           return company;  
         }  
         catch  
         {  
           MessageBox.Show("Warning: Company not set!");  
           company = "";  
           return company;  
         }  
       }  
       else company = "";  
       return company;  
     }  

No comments:

Post a Comment