Feedback

C# - Liefert eine DataTable mit allen Attributen eines cn-Names.

Veröffentlicht von am 16.03.2007
(1 Bewertungen)
Verwendung siehe Kommentar

Benötigt die Snippets "Liefert ein Attribut Objekt eines LDAP Objects (cn)" und "Konvertiert ein Byte Array in ein SID String."
/// <summary>
/// Liefert eine DataTable mit allen Attributen eines cn-Names.
/// </summary>
/// <param name="LDAPcn">Der cn-Name</param>
/// <param name="objectClass">"computer", "user", "group","organizationalunit" oder null.</param>
/// <returns>DataTable</returns>
/// <example>
///     DataTable dt = GetAllLDAPAttributs(Environment.UserName,"user");
///     this.dataGridView1.DataSource = dt;
///     this.dataGridView1.Columns[0].MinimumWidth = 150;
///     this.dataGridView1.Columns[1].MinimumWidth = 450;
///     this.dataGridView1.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
///     this.dataGridView1.Sort(dataGridView1.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
///     this.dataGridView1.Refresh();
/// </example>
public DataTable GetAllLDAPAttributs(string LDAPcn, string objectClass)
{
	DataTable dt = new DataTable();
	dt.Columns.Add(new DataColumn("AttributName", typeof(string)));
	dt.Columns.Add(new DataColumn("AttributValue", typeof(string)));
	dt.Columns.Add(new DataColumn("Type", typeof(string)));
	DataRow dr;
	
	object distinguishedName = GetLDAPValue(LDAPcn, "distinguishedName",objectClass, null);
	DirectoryEntry de = new DirectoryEntry("LDAP://" + (string)distinguishedName);
	foreach (string attributName in de.Properties.PropertyNames)
	{
		foreach (object Value in de.Properties[attributName])
		{
			dr = dt.NewRow();
			dr["AttributName"] = attributName;
			dr["Type"] = Value.GetType().ToString();
			switch (Value.GetType().ToString())
			{
				case "System.String":
					dr["AttributValue"] = (string)Value;
					break;
				case "System.Int32":
					dr["AttributValue"] = (int)Value;
					break;
				case "System.DateTime":
					dr["AttributValue"] = (DateTime)Value;
					break;
				case "System.__ComObject":
					object item = GetLDAPValue(LDAPcn,attributName,null,null);
					if (attributName == "badPasswordTime" | attributName == "lastLogon" | attributName == "lastLogonTimestamp" | attributName == "pwdLastSet" | attributName == "accountExpires")
					{
					   if (item.ToString() == "0")
					   {
						  dr["AttributValue"] = "(None)";
					   }
					   else if ((long)item >= System.Int64.MaxValue)
					   {
						   dr["AttributValue"] = "No Expiration";
					   }
					   else
					   {
						   dr["AttributValue"] = System.DateTime.FromFileTime((long)item);
					   }
					}
					break;
				case "System.Byte[]":
					byte[] ba = (byte[])Value;
					if (attributName == "objectSid")
					{
						dr["AttributValue"] = ConvertSid(ba);
					}
					else if (attributName == "objectGUID")
					{
						StringBuilder sb = new StringBuilder();
						for (int i = 0; i < ba.Length; i++)
						{
							sb.Append(ba[i].ToString("X2"));
						}
						dr["AttributValue"] = "{" + sb.ToString() + "}";
					}
					break;
		  }
		  dt.Rows.Add(dr);  
		}
	}
	return dt;
}
Abgelegt unter LDAP.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!