如何在c#中获取活动目录用户的所有属性

本文关键字:用户 属性 活动 获取 | 更新日期: 2023-09-27 18:07:02

我一直在寻找一种使用c#代码的解决方案,可以查询Active Directory用户注册到它的所有属性,无论它们是否具有NULL值。这些属性可以通过域服务器上ADSI Edit中用户属性中的Attribute编辑器选项卡查看。

ADSI编辑中的AD用户属性

我需要动态检索这些属性,这意味着我可能无法通过MSDN上的ADSI文档可靠地获得这些属性名称,因为并非所有这些属性都可能是特定于用户对象的:https://msdn.microsoft.com/en-us/library/ms675090 (v = vs.85) . aspx

下面是我到目前为止尝试过的,但只得到了用户对象的一小部分属性:

  1. PS命令Get-ADUser -Identity administrator -Properties:这检索了属性的一个很好的部分,但不是几乎所有的,我不知道什么。net类和方法被调用在这个命令,因为TypeName = Microsoft.ActiveDirectory.Management.ADUser,它不存在于。net框架。我如何在PS中看到。net中使用的具体方法?

  2. c#调用这个方法

    public bool GetUserAttributes(out List<string> userAttributes, string userName)
    {
        userAttributes = new List<string>();
        var valueReturn = false;
        try
        {
            const string pathNameDomain = "LDAP://test.local";
            var directoryEntry = new DirectoryEntry(pathNameDomain);
            var directorySearcher = new DirectorySearcher(directoryEntry)
            {
                Filter = "(&(objectClass=user)(sAMAccountName=" + userName + "))"
            };
            var searchResults = directorySearcher.FindAll();
            valueReturn = searchResults.Count > 0;
            StreamWriter writer = new StreamWriter("C:''LDAPGETUSERADEXAMPLE.txt");
            foreach (SearchResult searchResult in searchResults)
            {
                foreach (var valueCollection in searchResult.Properties.PropertyNames)
                {
                    userAttributes.Add(valueCollection.ToString() + " = " + searchResult.Properties[valueCollection.ToString()][0].ToString());
                    try
                    {
                        writer.WriteLine("Bruger attribut:" + valueCollection);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
    
  3. c#调用这个方法

    public List<string> GetADUserAttributes()
    {
        string objectDn = "CN=testuser,OU=TEST,DC=test,DC=local";
        DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + objectDn);
        List<string> attributes = new List<string>();
        foreach (string attribute in objRootDSE.Properties.PropertyNames)
        {
            attributes.Add(attribute);
        }
        return attributes;
    }
    

我应该做些什么来不过滤掉我试图从中检索的用户对象的任何属性?

我知道活动目录默认情况下只显示默认属性或有值的属性,我正在努力克服这个限制。

编辑1:

我暂时推迟了具体的问题。我一直在尝试对这些方法中的哪一种进行基准测试,以最快的速度检索(读取操作)10,000个单独AD用户的SAM帐户名称,例如"testuser",我基准测试的方法如下:

  1. 完成时间:大约500毫秒:ADSI - system.directoryservices
  2. 完成时间:大约2700毫秒:Principal - searcher system.directoryservices.accountmanagement
  3. 时间完成:关于不工作:LDAP - System.DirectoryServices.Protocols
  4. SQL - System.Data.SqlClient

我正在从我正在查询的域中的工作站- Windows 10机器查询用户信息。工作站(4vcpu)、数据中心(2vpu)和数据库(2vcpu)服务器作为Hyper V虚拟机运行。

如何在c#中获取活动目录用户的所有属性

任何类可以拥有的所有属性都在Active Directory架构中定义

使用这个来查询用户类。然后调用GetAllProperties方法

var context = new DirectoryContext(DirectoryContextType.Forest, "amber.local");
using (var schema = System.DirectoryServices.ActiveDirectory.ActiveDirectorySchema.GetSchema(context))
{
    var userClass = schema.FindClass("user");
    foreach (ActiveDirectorySchemaProperty property in userClass.GetAllProperties())
    {
        // property.Name is what you're looking for
    }
}

但是AD模式可能因AD环境而异。例如,第三方程序或Exchange Server可以使用自定义属性扩展模式。这意味着具有预定义列的解决方案只适用于特定的环境。