System.DirectoryServices vs system.directoryservices.accountmanagement

二次信任 提交于 2020-01-14 22:48:05

问题


I have an array (propertyList) that contains the names of certain 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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/1546871/system-directoryservices-vs-system-directoryservices-accountmanagement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!