Feedback

C# - Return DataTable with LDAP UserAttributs

Veröffentlicht von am 19.03.2007
(1 Bewertungen)
Return DataTable with LDAP UserAttributs.
use: see Example
GFU-Schulungen  [Anzeige]

C# 2017/2015/2013 Grundlagen

Nach Abschluss des Kurses sind Ihnen die Datentypen und Klassenbibliotheken von C# bekannt. Außerdem sind Sie in der Lage, mit Variablen umzugehen und deren Typen zu konvertieren. Sie definieren Namespaces und setzen diese ein, Sie erstellen eigene Klassen  und bauen Trouble Shooting ein.

VB.NET 2017/2015/2013 Aufbau

Nach dieser Schulung können Sie mittels objektorientierter Modelle in VB.NET 2017/2015/2013 wiederverwendbare Elemente eigenständig erstellen.

/// <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;
}
Abgelegt unter LDAP.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!