Tuesday, July 14, 2015

Read User Properties From Active Directory

Read User Properties From Active Directory:-
lblMessage.Text = GetEmployeeID("name@company.com"); 
internal static string GetEmployeeID (string strmail)
        {
            StringBuilder sbQuery = new StringBuilder();
            string stremployeeid = string.Empty;           
            string strLdapPath = "LDAP://DC=company_domain,DC=com";
            try
            {
                if (string.IsNullOrEmpty(Convert.ToString(sbQuery)))
                {
                    sbQuery.Append("(&(objectClass=user)(|(mail=" + strmail + ")");
                }
                sbQuery.Append("))");
                DirectoryEntry entry = new DirectoryEntry(strLdapPath);
                DirectorySearcher userSearch = new DirectorySearcher(entry);
                userSearch.Filter = Convert.ToString(sbQuery);
                userSearch.PropertiesToLoad.Add("mail");
                userSearch.PropertiesToLoad.Add("employeeid");
                SearchResultCollection sresultcoll = userSearch.FindAll();
                foreach (SearchResult sresult in sresultcoll)
                {
                    if (sresult != null)
                    {
                        if (sresult.Properties["mail"].Count == 1)
                        {
                            stremployeeid = sresult.Properties["employeeid"].Count > 0 ? Convert.ToString(sresult.Properties["employeeid"][0]) : string.Empty;
                            if (stremployeeid != null && stremployeeid.Length > 0)
                            {
                                return stremployeeid;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return stremployeeid;
        }

No comments:

Post a Comment

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   &qu...

Popular posts