【问题标题】:System.DirectoryServices vs system.directoryservices.accountmanagementSystem.DirectoryServices 与 system.directoryservices.accountmanagement
【发布时间】:2009-10-10 02:33:05
【问题描述】:

我有一个数组 (propertyList),其中包含我要检索其数据的某些 Active Directory 属性的名称。使用 Ironpython 和 .NET 库 System.DirectoryServices 我以这种方式解决了要加载的属性的检索:

for propertyActDir in propertyList:
    obj.PropertiesToLoad.Add(propertyActDir)
res = obj.FindAll()
myDict = {}
for sr in res:
    for prop in propertyList:
        myDict[prop] = getField(prop,sr.Properties[prop][0])

函数 getField 是我的。如何使用图书馆 system.directoryservices.accountmanagement 解决同样的情况?我认为这是不可能的。

谢谢。

【问题讨论】:

    标签: active-directory


    【解决方案1】:

    是的,您是对的 - System.DirectoryServices.AccountManagement 建立在 System.DirectoryServices 之上,并在 .NET 3.5 中引入。它使常见的 Active Directory 任务更容易。如果您需要任何特殊属性,则需要回退到 System.DirectoryServices。

    查看此 C# 代码示例以了解用法:

    // Connect to the current domain using the credentials of the executing user:
    PrincipalContext currentDomain = new PrincipalContext(ContextType.Domain);
    
    // Search the entire domain for users with non-expiring passwords:
    UserPrincipal userQuery = new UserPrincipal(currentDomain);
    userQuery.PasswordNeverExpires = true;
    
    PrincipalSearcher searchForUser = new PrincipalSearcher(userQuery);
    
    foreach (UserPrincipal foundUser in searchForUser.FindAll())
    {
        Console.WriteLine("DistinguishedName: " + foundUser.DistinguishedName);
    
        // To get the countryCode-attribute you need to get the underlying DirectoryEntry-object:
        DirectoryEntry foundUserDE = (DirectoryEntry)foundUser.GetUnderlyingObject();
    
        Console.WriteLine("Country Code: " + foundUserDE.Properties["countryCode"].Value);
    }
    

    【讨论】:

    • 感谢您的示例 Per。正如您所说,有必要回退到 System.DirectoryServices,但在这种情况下扩展目录对象类:UserPrincipal、GroupPrincipal 和 ComputerPrincipal,如 msdn.microsoft.com/en-us/library/bb552835.aspx 所示。
    【解决方案2】:

    System.DirectoryServices.AccountManagement(优秀的MSDN article on it here)旨在帮助您更轻松地管理用户和组,例如

    • 查找用户和组
    • 创建用户和组
    • 为用户和组设置特定属性

    不是旨在像您描述的那样处理“通用”物业管理 - 在这种情况下,只需继续使用 System.DirectoryServices,没有什么能阻止您这样做!

    马克

    【讨论】:

      猜你喜欢
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多