Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 70
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

Liefert ein Dictionary von Attributen von einem LDAP Object


Autor: stol
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 4908
  
Kick it on dotnet-kicks.de  

Beschreibung:

Habe mal das Snippet "Liefert ein Dictionary Object von LDAP Userattributen" dahingend verallgemeinert, so dass es auf alle LDAP-Objekte angewendet werden kann

Abgelegt unter: LDAP.



C#
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
68
69
70
/// <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;
	}
}
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.