System.DirectoryServices vs system.directoryservices.accountmanagement

Tri*_*tan 3 active-directory

我有一个数组(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])
Run Code Online (Sandbox Code Playgroud)

函数getField是我的.如何使用库system.directoryservices.accountmanagement解决相同的情况?我认为这是不可能的.

谢谢.

Per*_*alt 6

是的,你是对的 - 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);
}
Run Code Online (Sandbox Code Playgroud)