1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
/// <summary>
/// Liefert ein Object mit dem einzelnen LDAP-Wert für ein eindeutiges LDAP-Object (cn).
/// Ist null wenn a)das Object nicht gefunden wurde oder b)mehrere Einträge existieren.
/// </summary>
/// <param name="LDAPcn">Der cn-Name.</param>
/// <param name="Property">Das gewünschte Property-Attribut.</param>
/// <param name="objectClass">Die AD ObjectClass. Mögliche Werte "computer", "user", "group","organizationalunit" oder null. </param>
/// <param name="LDAPEntryPoint">Der LDAP Einstiegspunkt oder null</param>
/// <example>
/// object up = GetLDAPValue(Environment.UserName, "mail","user",null);
/// </example>
public object GetLDAPValue(string LDAPcn, string Property, string objectClass, string LDAPEntryPoint)
{
DirectorySearcher Searcher = new DirectorySearcher();
SearchResultCollection SearchResults;
if (objectClass == null)
{
Searcher.Filter = "(cn=" + LDAPcn + ")";
}
else
{
objectClass = objectClass.ToUpper();
switch (objectClass)
{
case "COMPUTER":
Searcher.Filter = "(&(objectClass=computer)(cn=" + LDAPcn + "))";
break;
case "USER":
Searcher.Filter = "(&(objectClass=user)(cn=" + LDAPcn + "))";
break;
case "GROUP":
Searcher.Filter = "(&(objectClass=group)(cn=" + LDAPcn + "))";
break;
case "ORGANIZATIONALUNIT":
Searcher.Filter = "(&(objectClass=organizationalUnit)(cn=" + LDAPcn + "))";
break;
}
}
Searcher.PropertiesToLoad.Add(Property);
Searcher.SearchScope = SearchScope.Subtree;
if (LDAPEntryPoint == null)
{
System.DirectoryServices.DirectoryEntry adsiRoot = new System.DirectoryServices.DirectoryEntry("LDAP://RootDSE");
LDAPEntryPoint = "LDAP://" + adsiRoot.Properties["defaultNamingContext"][0];
}
Searcher.SearchRoot = new DirectoryEntry(LDAPEntryPoint);
SearchResults = Searcher.FindAll();
if (SearchResults.Count > 1)
{
return null;
}
else
{
foreach (SearchResult OutPut in SearchResults)
{
try
{
return OutPut.Properties[Property][0];
}
catch
{
return null;
}
}
}
return null;
}
|