1: public static StringDictionary GetGAL(string UserName, string Password)
2: {
3: try
4: {
5: StringDictionary ReturnArray = new StringDictionary();
6: DirectoryEntry deDirEntry = new DirectoryEntry("LDAP://server",
7: UserName,
8: Password,
9: AuthenticationTypes.Secure);
10: DirectorySearcher mySearcher = new DirectorySearcher(deDirEntry);
11: mySearcher.PropertiesToLoad.Add("cn");
12: mySearcher.PropertiesToLoad.Add("mail");
13:
14: string sFilter = String.Format("(&(mailnickname=*)(|(objectcategory=user)(objectcategory=group)))");
15:
16: mySearcher.Filter = sFilter;
17: mySearcher.Sort.Direction = SortDirection.Ascending;
18: mySearcher.Sort.PropertyName = "cn";
19: mySearcher.PageSize = 1000;
20:
21: SearchResultCollection results;
22: results = mySearcher.FindAll();
23:
24: foreach (SearchResult resEnt in results)
25: {
26: ResultPropertyCollection propcoll = resEnt.Properties;
27: string Name = "";
28: string Mail = "";
29: foreach (string key in propcoll.PropertyNames)
30: {
31: if (key == "cn")
32: {
33: foreach (object values in propcoll[key])
34: {
35: Name = values.ToString();
36: }
37: }
38: else if (key == "mail")
39: {
40: foreach (object values in propcoll[key])
41: {
42: Mail = values.ToString();
43: }
44: }
45: }
46: if (!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(Mail))
47: {
48: ReturnArray.Add(Name, Mail);
49: }
50: }
51: return ReturnArray;
52: }
53: catch
54: {
55: return null;
56: }
57: }