Liefert ein Dictionary Object von LDAP Userattributen
/// <summary>
/// Liefert ein Dictionary Object von LDAP Userattributen
/// </summary>
/// <param name="UserName">Der NT UserAccount</param>
/// <param name="UserProperties">Liste der zu ermittelnden Properties
/// <example>
/// System.Collections.Generic.Dictionary<string, object> UserAttributs = GetUserProperties(Environment.UserName,new List<string>(new string[] {"mail","cn", "lastlogon","distinguishedName"}));
/// </example>
/// <returns></returns>
public System.Collections.Generic.Dictionary<string, object> GetUserProperties(string UserName, List<string> UserProperties)
{
DirectorySearcher Searcher = new DirectorySearcher();
SearchResultCollection SearchResults = null;
Searcher.Filter = "(&(objectclass=user)(cn=" + UserName + "))";
for (int i = 0; i <= UserProperties.Count - 1; i++)
{
Searcher.PropertiesToLoad.Add(UserProperties[i]);
}
Searcher.SearchScope = SearchScope.Subtree;
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> UserPropertiesResults = new System.Collections.Generic.Dictionary<string, object>();
foreach (SearchResult OutPut in SearchResults)
{
for (int i = 0; i <= UserProperties.Count - 1; i++)
{
try
{
UserPropertiesResults.Add(UserProperties[i], OutPut.Properties[UserProperties[i]][0]);
}
catch
{
UserPropertiesResults.Add(UserProperties[i], null);
}
}
}
return UserPropertiesResults;
}
}
Kommentare zum Snippet