Search This Blog

Sunday, July 30, 2017

Get LDAP User Information Using C# with LDAP Connection

string txtEmp;
            string sa = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
            txtEmp = sa.Split('\\')[1];
            string strDLdap = ConfigurationManager.AppSettings["myLDAPString"].ToString();
            DirectoryEntry connection = new DirectoryEntry(strDLdap);
            string strDLdAPUser = ConfigurationManager.AppSettings["myLDAPUser"].ToString();
            string strDLdAPPass =ConfigurationManager.AppSettings["myLDAPPassword"].ToString();
            connection.AuthenticationType = AuthenticationTypes.Secure;
            connection.Username = strDLdAPUser;
            connection.Password = strDLdAPPass;
            DirectorySearcher dssearch = new DirectorySearcher(connection);
            dssearch.Filter = "(sAMAccountName=" + txtEmp + ")";
            SearchResult sresult = dssearch.FindOne();
            DirectoryEntry dsresult = sresult.GetDirectoryEntry();
            string strfirstName = dsresult.Properties["givenName"][0].ToString();
            string strlastName = dsresult.Properties["sn"][0].ToString();
            string Name = strfirstName + "" + strlastName;
            string strRequestedBy = dsresult.Properties["mail"][0].ToString();
            string strComapnay = dsresult.Properties["company"][0].ToString();
            displayName = dsresult.Properties["displayName"][0].ToString();
            InsertUserInfo(displayName, strComapnay, DateTime.Now.ToString("MM-dd-yyyy"), strRequestedBy);
 

No comments:

Post a Comment