/// <summary>
/// Liefert ein Dictionary von einem LDAP Object
/// </summary>
/// <param name="LDAPcn">Der eindeutige LDAP cn</param>
/// <param name="objectClass">Mögliche Werte "computer", "user", "group","organizationalunit" oder null.</param>
/// <param name="LDAPProperties">Liste der zu ermittelnden Properties
/// <example>
/// System.Collections.Generic.Dictionary<string, object> LDAPAttributs = GetLDAPAttributes(Environment.UserName,"user",new List<string>(new string[] {"mail","cn", "lastlogon","distinguishedName"}));
/// System.Collections.Generic.Dictionary<string, object> LDAPAttributs1 = GetLDAPAttributes(Environment.MachineName, "computer", new List<string>(new string[] { "operatingSystem", "pwdLastSet", "lastlogon", "distinguishedName" }));
/// </example>
/// <returns></returns>
public System.Collections.Generic.Dictionary<string, object> GetLDAPAttributes(string LDAPcn, string objectClass, List<string> LDAPProperties)
{
DirectorySearcher Searcher = new DirectorySearcher();
SearchResultCollection SearchResults = null;
for (int i = 0; i <= LDAPProperties.Count - 1; i++)
{
Searcher.PropertiesToLoad.Add(LDAPProperties[i]);
}
Searcher.SearchScope = SearchScope.Subtree;
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;
}
}
System.DirectoryServices.DirectoryEntry adsiRoot = new System.DirectoryServices.DirectoryEntry("LDAP://RootDSE");
Searcher.SearchRoot = new DirectoryEntry("LDAP://" + adsiRoot.Properties["defaultNamingContext"][0]);
SearchResults = Searcher.FindAll();
if (SearchResults.Count > 1)
{
return null;
}
else
{
System.Collections.Generic.Dictionary<string, object> LDAPPropertiesResults = new System.Collections.Generic.Dictionary<string, object>();
foreach (SearchResult OutPut in SearchResults)
{
for (int i = 0; i <= LDAPProperties.Count - 1; i++)
{
try
{
LDAPPropertiesResults.Add(LDAPProperties[i], OutPut.Properties[LDAPProperties[i]][0]);
}
catch
{
LDAPPropertiesResults.Add(LDAPProperties[i], null);
}
}
}
return LDAPPropertiesResults;
}
}