开发者

System.DirectoryServices vs system.directoryservices.accountmanagement

I have an array (propertyList) that contains the names of cer开发者_如何转开发tain Active Directory properties whose data I want to retrieve. Using Ironpython and .NET library System.DirectoryServices I solve the retrieval of properties to be loaded in this way:

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])

The function getField is mine. How can I solve the same situation using the library system.directoryservices.accountmanagement? I think it is not possible.

Thanks.


Yes, you're right - System.DirectoryServices.AccountManagement builds on System.DirectoryServices and was introduced with .NET 3.5. It makes common Active Directory tasks easier. If you need any special properties you need to fall back to System.DirectoryServices.

See this C# code sample for usage:

// 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);
}


System.DirectoryServices.AccountManagement (excellent MSDN article on it here) is designed to help you more easily manage user and groups, e.g.

  • find users and groups
  • create users and groups
  • set specific properties on users and groups

It is not designed to handle "generic" property management like you describe - in that case, simply keep on using System.DirectoryServices, there's nothing stopping you from doing this!

Marc

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜