如何从 C# 中的 Active Directory 用户获取所有属性

Cas*_*spi 3 c# powershell attributes ldap active-directory

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

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;
                    }
                }
            }
    
    Run Code Online (Sandbox Code Playgroud)
  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;
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

我知道默认情况下 Active Directory 只会显示默认属性或其中有值的属性,我正在尝试克服此限制。

编辑 1:

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

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

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

old*_*ets 5

任何类可以拥有的所有属性都在 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
    }
}
Run Code Online (Sandbox Code Playgroud)

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