Home
Manage Your Code
Snippet: LDAP Entry (C#)
Title: LDAP Entry Language: C#
Description: Read AD Entry using LDAP Views: 209
Author: Jukka Kotamäki Date Added: 11/5/2008
Copy Code  
1string LDAPConn = ConfigurationManager.AppSettings["LDAPConnection"].ToString();
2string username = ConfigurationManager.AppSettings["LDAPUser"].ToString();
3string password = ConfigurationManager.AppSettings["LDAPPassword"].ToString();
4            
5DirectoryEntry entry = new DirectoryEntry(LDAPConn, username, password);
6            
7DirectorySearcher search = new DirectorySearcher(entry);            
8search.Filter = "(SAMAccountName=" + this.User.Identity.Name.Split(new char[] {'\\'})[1] + ")";
9
10search.PropertiesToLoad.Add("$url$");
11SearchResult result = search.FindOne();
12if (result != null)
13{
14    foreach (string key in result.Properties.PropertyNames)
15    {                 
16        foreach (Object propValue in result.Properties[key])
17        {
18            if (propValue.ToString().IndexOf(MOSSAddress) >= 0)
19            {   
20                string value = propValue.ToString();                           
21            }            
22        }
23    }                
24}