Monday, April 14, 2014

Active Directory - Getting a user's Contact Info from AD

Saving a snippet of code...

        [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted = true)]
        private void accessAdEmpDetails()
        {
            string[] TempUserId = new string[2];

            // Username/Password with rights to query AD
            string activeDirUsername = ConfigurationManager.AppSettings["adUserName"];
            string activeDirPassword = ConfigurationManager.AppSettings["adPassword"];

            string[] activeDirServerNames = new string[] { ConfigurationManager.AppSettings["adLink1"], 
                                                       ConfigurationManager.AppSettings["adLink2"], 
                                                       ConfigurationManager.AppSettings["adLink3"], 
                                                       ConfigurationManager.AppSettings["adLink4"], 
                                                       ConfigurationManager.AppSettings["adLink5"] };

            WindowsIdentity WindowsIdentity = WindowsIdentity.GetCurrent();
            WindowsPrincipal WindowsPrincipal = new WindowsPrincipal(WindowsIdentity);

            char[] SeparatorSlash = { '\\' };

            TempUserId = Page.User.Identity.Name.Split(SeparatorSlash, 2);

            // Keeping user Id into the Session variable to furthur use.
            Session["USER_ID"] = TempUserId[1];

            if (WindowsIdentity.AuthenticationType.ToUpper().Equals("KERBEROS"))
            {
                // Loop through AD servers
                foreach (string serverName in activeDirServerNames)
                {
                    // Get a DirectoryEntry from each AD server
                    using (DirectoryEntry entry = new DirectoryEntry(serverName))
                    {
                        entry.Username = activeDirUsername; //User to access AD
                        entry.Password = activeDirPassword; //Pwd to access AD

                        using (DirectorySearcher searcher = new DirectorySearcher(entry))
                        {
                            searcher.Filter = "(&(objectClass=user)(samaccountname=" + TempUserId[1] + "))";

                            SearchResult Result = searcher.FindOne();

                            if (Result != null)
                            {
                                if (Result.GetDirectoryEntry().Properties["departmentNumber"].Value != null)
                                {
                                    userDept = Result.GetDirectoryEntry().Properties["departmentNumber"].Value.ToString();
                                }

                                if (!string.IsNullOrEmpty(Result.GetDirectoryEntry().Properties["name"].Value.ToString()))
                                {
                                    userName = Result.GetDirectoryEntry().Properties["name"].Value.ToString();
                                }

                                if (Result.GetDirectoryEntry().Properties["physicalDeliveryOfficeName"].Value != null)
                                {
                                    userLocation = Result.GetDirectoryEntry().Properties["physicalDeliveryOfficeName"].Value.ToString();
                                }

                                if (Result.GetDirectoryEntry().Properties["mail"].Value != null)
                                {
                                    userMail = Result.GetDirectoryEntry().Properties["mail"].Value.ToString();
                                }

                                if (Result.GetDirectoryEntry().Properties["telephoneNumber"].Value != null)
                                {
                                    userPhone = Result.GetDirectoryEntry().Properties["telephoneNumber"].Value.ToString();
                                }
                            }
                        }
                    }
                }
            }
        }

No comments: