答案 0 :(得分:1)
这是我需要的解决方案:
List<string> userPropertyList = new List<string>();
ActiveDirectorySchema currSchema = ActiveDirectorySchema.GetCurrentSchema();
ActiveDirectorySchemaClass collection = currSchema.FindClass("user");
ReadOnlyActiveDirectorySchemaPropertyCollection properties = collection.GetAllProperties();
IEnumerator enumerator = properties.GetEnumerator();
while (enumerator.MoveNext())
{
userPropertyList.Add(enumerator.Current.ToString());
}
要获取组的所有可能属性,只需更改&#34; user&#34;到&#34; group&#34;。 此ldap查询也包括子类的所有属性。 对于例如询问所有属性的类&#34;用户&#34;将包括&#34; tob&#34;,&#34; person&#34;和&#34; organizationalPerson&#34;。
向abhitalks寻求解决方案的提示。
答案 1 :(得分:0)
这个片段来自我的一个旧项目,我需要做几乎相同的事情。此代码段是更大的测试ASP.Net应用程序的一部分,因此您可以看到response.write
。
很抱歉,我在VB.Net中有它,但我相信你能用C#来实现它。
Sub GetAllUserInfo(ByVal userName As String)
Dim strGroup As String
Dim adRoot As New DirectoryServices.DirectoryEntry("LDAP://domain.local/DC=domain,DC=local")
Dim adSearch As New DirectoryServices.DirectorySearcher(adRoot)
Dim adResult As DirectoryServices.SearchResult
adSearch.Filter = "(sAMAccountName=" + userName + ")"
adSearch.PropertiesToLoad.Add("cn")
adResult = adSearch.FindOne()
Response.Write("<table>")
For Each x As DirectoryServices.PropertyValueCollection In adResult.GetDirectoryEntry.Properties
Response.Write("<tr><td>")
Response.Write(x.PropertyName)
Response.Write("</td><td>")
Response.Write(x.Value.ToString)
Response.Write("</td></tr>")
If x.PropertyName = "memberOf" Then
For Each s As String In x.Value
Response.Write("<tr><td>")
Response.Write("Groups: ")
Response.Write("</td><td>")
strGroup = Mid(s, InStr(s, "CN=") + 3, (InStr(InStr(s, "CN=") + 3, s, ",") - (InStr(s, "CN=") + 3)))
Response.Write(strGroup)
Response.Write("</td></tr>")
Next
End If
Next
Response.Write("</table>")
End Sub
确定。在这里你进入C#,使用在线转换器。
public void GetAllUserInfo(string userName)
{
string strGroup = null;
DirectoryServices.DirectoryEntry adRoot = new DirectoryServices.DirectoryEntry("LDAP://domain.local/DC=domain,DC=local");
DirectoryServices.DirectorySearcher adSearch = new DirectoryServices.DirectorySearcher(adRoot);
DirectoryServices.SearchResult adResult = default(DirectoryServices.SearchResult);
adSearch.Filter = "(sAMAccountName=" + userName + ")";
adSearch.PropertiesToLoad.Add("cn");
adResult = adSearch.FindOne();
Response.Write("<table>");
foreach (DirectoryServices.PropertyValueCollection x in adResult.GetDirectoryEntry.Properties) {
Response.Write("<tr><td>");
Response.Write(x.PropertyName);
Response.Write("</td><td>");
Response.Write(x.Value.ToString);
Response.Write("</td></tr>");
if (x.PropertyName == "memberOf") {
foreach (string s in x.Value) {
Response.Write("<tr><td>");
Response.Write("Groups: ");
Response.Write("</td><td>");
strGroup = Strings.Mid(s, Strings.InStr(s, "CN=") + 3, (Strings.InStr(Strings.InStr(s, "CN=") + 3, s, ",") - (Strings.InStr(s, "CN=") + 3)));
Response.Write(strGroup);
Response.Write("</td></tr>");
}
}
}
Response.Write("</table>");
}
更新:
上述代码仅检索具有值集的属性。如果需要枚举LDAP模式中的所有属性,则需要查看ActiveDirectorySchema
命名空间中的ActiveDirectorySchemaClass
和DirectoryServices.ActiveDirectory
类。
获取架构:
ActiveDirectorySchema currentSchema = ActiveDirectorySchema.GetCurrentSchema();
获得架构后,您可以看到类:
ActiveDirectorySchemaClass objClass = currentSchema.FindClass("ldapDisplayName");
其中, ldapDisplayName 是AD对象名称,如“person”,“computer”等。
现在,您可以枚举属性。确保列举了该类的MandatoryProperties
和OptionalProperties
属性:
objClass.MandatoryProperties
objClass.OptionalProperties
希望有所帮助。