Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1563 | Anzahl registrierter User: 1895 | Besucher online: 106
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)

Return DataTable with LDAP UserAttributs


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

Beschreibung:

Return DataTable with LDAP UserAttributs.
use: see Example


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>
/// Return DataTable with LDAP UserAttributs
/// </summary>
/// <param name="SearchPerson">SearchPattern for sn or cn</param>
/// <param name="List<string>">List of Attributs</param>
/// <param name="GlobalCatalog">False for Local Domain, True for Search in GlobalCatalog</param>
/// <returns></returns>
/// <example>
///    DataTable dt = GetLDAPUserAttributs("Fischer", new List<string>(new string[] { "givenName", "sn", "cn", "telephoneNumber", "department", "physicalDeliveryOfficeName", "mail", "distinguishedName" }), true);
///    System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
///    dataGridViewCellStyle1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(224)))), ((int)(((byte)(192)))));
///    this.dataGridView1.AlternatingRowsDefaultCellStyle = dataGridViewCellStyle1;
///    this.dataGridView1.ReadOnly = true;
///    this.dataGridView1.AllowUserToAddRows = false;
///    this.dataGridView1.DataSource = dt;
///    this.dataGridView1.Columns[0].MinimumWidth = 120;
///    this.dataGridView1.Columns[1].HeaderText = "Vorname";  this.dataGridView1.Columns[1].MinimumWidth = 80;
///    this.dataGridView1.Columns[2].HeaderText = "Nachname"; this.dataGridView1.Columns[2].MinimumWidth = 90;
///    this.dataGridView1.Columns[3].HeaderText = "Loginname"; this.dataGridView1.Columns[3].MinimumWidth = 80;
///    this.dataGridView1.Columns[4].HeaderText = "Telefon"; this.dataGridView1.Columns[4].MinimumWidth = 120;
///    this.dataGridView1.Columns[5].HeaderText = "Abteilung"; this.dataGridView1.Columns[5].MinimumWidth = 120;
///    this.dataGridView1.Columns[6].HeaderText = "Standort"; this.dataGridView1.Columns[6].MinimumWidth = 120;
///    this.dataGridView1.Columns[7].HeaderText = "e-Mail"; this.dataGridView1.Columns[7].MinimumWidth = 120;
///    this.dataGridView1.Columns[8].HeaderText = "DN"; this.dataGridView1.Columns[8].Visible = false;
///    this.dataGridView1.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
///    this.dataGridView1.Sort(dataGridView1.Columns[1], System.ComponentModel.ListSortDirection.Ascending);
///    this.dataGridView1.Refresh();
/// </example>
public DataTable GetLDAPUserAttributs(string SearchPerson, List<string> LDAPProperties, bool GlobalCatalog)
{
	SearchPerson = SearchPerson + "*";
	DataTable dt = new DataTable();
	DataRow dr;
	DirectorySearcher Searcher = new DirectorySearcher();
	SearchResultCollection SearchResults = null;
	Searcher.Filter = "(&(objectClass=user)(|(displayName=" + SearchPerson + ")(cn=" + SearchPerson + ")(sAMAccountName=" + SearchPerson + ")))";
	Searcher.SearchScope = SearchScope.Subtree;
	dt.Columns.Add(new DataColumn("Domain", typeof(string)));
	foreach (string Name in LDAPProperties)
	{
		dt.Columns.Add(new DataColumn(Name, typeof(string)));
		Searcher.PropertiesToLoad.Add(Name);
	}
	if (GlobalCatalog)
	{
		Domain d = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain();
		GlobalCatalog gc = d.Forest.FindGlobalCatalog();
		Searcher.SearchRoot = new DirectoryEntry(@"GC://" + gc.Name);
	} else
	{
		System.DirectoryServices.DirectoryEntry adsiRoot = new System.DirectoryServices.DirectoryEntry(@"LDAP://RootDSE");
		Searcher.SearchRoot = new DirectoryEntry(@"LDAP://" + adsiRoot.Properties["defaultNamingContext"][0]);
	}
	SearchResults = Searcher.FindAll();
	foreach (SearchResult Result in SearchResults)
	{
		dr = dt.NewRow();
		string Domain = Result.Path;
		Domain = Domain.Substring(Domain.IndexOf("DC="));
		Domain = Domain.Replace("DC=","");
		Domain = Domain.Replace(",",".");
		dr["Domain"] = Domain;
		foreach (string Name in LDAPProperties)
		{
			if (Result.Properties[Name].Count >=1) dr[Name] = Result.Properties[Name] [0];
		}
		dt.Rows.Add(dr);
	}
	return dt;
}
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.