2013-01-17 242 views
8

我正在使用System.DirectoryServices.AccountManagement.dll来处理Active Directory 以获取“域用户”组中的所有用户。从Active Directory获取已启用帐户

这是返回域中的所有用户,但我只需要获取启用的用户。

下面是一些示例代码:

List<string> users = new List<string>(); 

PrincipalContext pcContext = GetPrincipalContext(); 

GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcContext, 
           IdentityType.Name, 
           "Domain Users"); 

foreach (Principal user in grp.GetMembers(true).OfType<UserPrincipal>()) 
{ 
    if (user.Enabled != false) 
    { 
     users.Add(user.Name); 
    } 
} 

其他组做工精细,但当组是“域用户”,该Enabled属性的值是false于所有用户。这使得无法区分启用和禁用的用户而无需为每个用户进行进一步的查询。

+0

你只需要帐号名吗?是否需要使用AccountManagement命名空间?除了存储名称之外,您是否对UserPrincipal对象执行任何操作?我只问,因为您的需求可能会更好地使用DirectoryServices命名空间和DirectorySearcher与LDAP过滤器如下所示:(&(objectclass = user)(memberOf = CN = Domain Users,dc = company,dc = com) (userAccountControl:1.2.840.113556.1.4.803:= 2)))这将返回该组中启用的所有用户。 – randcd

+1

randcd:我宁愿使用AM命名空间,因为它提供了一个非常干净的API。我当然可以使用LDAP,但是一切都变得更加复杂(特别是在用户只是域用户的成员的情况下,因为这是他们的主要组,因为它没有在'memberOf'中列出)。 – RobSiklos

+0

ForEach循环中的“Principal”实际上应该是“UserPrincipal” - “Enabled”不是可在Princpal对象上访问的方法或属性。至少不在.net 4.6.1中。这可以通过System.DirectoryServices.AccountManagement命名空间完成。 – DtechNet

回答

1

UserPrinciple对象为此具有bool Enabled属性。

http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal_properties.aspx

// Add this to GetUserDetails 
objUserDetails.EmployeeId = UserPrinical.EmployeeId; 


// Then condition the add to only add enabled 
if (objUserDetails.Enabled) { 
    objUserDetails.Add(GetUserDetails(p.Name)); 
} 
+0

这并不能解决问题,因为当被查询的组是“域用户”时,“Enabled”属性似乎没有被正确填充。 – RobSiklos

0

有一个在MSDN page of the Enabled property了一句话说:

如果委托人没有在店里坚持,这个属性返回null。在委托人被保留后,默认的启用设置取决于商店。 AD DS和AD LDS存储会在新主体持续存在时禁用新主体,而SAM会在新主体持续存在时启用新主体。应用程序只能将该属性设置为在商店中保存后的值。

也许这是相关的,如果默认为false?

此外,MSDN论坛上有一篇关于UserPrincipal.Enabled returns False for accounts that are in fact enabled?的帖子,这听起来与您的问题非常相似。根据这个帖子,这里可能有一个解决办法:

我想我误解了。无视我之前发布的内容。我想我 知道发生了什么。 GetMembers方法显然不是加载 UserPrincipal数据。我不知道是否有更好的解决方案,但 以下工作(至少在我的AD):

foreach (UserPrincipal user in group.GetMembers(false)) 
{ 
    UserPrincipal tempUser = UserPrincipal.FindByIdentity(context, user.SamAccountName); 
    // use tempUser.Enabled 
    // other code here 
} 
+0

用户已被保留,因此您答案的第一部分与这个问题。至于其他方面,是的 - 我知道我可以做到这一点 - 这个问题表明,我们希望对Enabled属性有一个正确的值,而不必做任何额外的查询(特别是不是每个用户都有一个像这样的foreach循环)。你从甚至引用的帖子说这个解决方案太慢而无用。 – RobSiklos

1

解决此问题的方法可能是使用PrincipalSearcher使用户第一次搜索然后用信安的方法IsMemberOf()

List<string> users = List<string>(); 
PrincipalContext pcContext = GetPrincipalContext(); 
GroupPrincipal grp = GroupPrincipal.FindByIdentity(pcContext, IdentityType.Name, "Domain Users"); 
UserPrincipal searchFilter = new UserPrincipal(pcContext){ Enabled = true } 
PrincipalSearcher searcher = new PrincipalSearcher(searchFilter); 
PrincipalSearchResult<Principal> results = searcher.FindAll(); 
foreach (Principal user in results) 
    if (user.IsMemberOf(grp)) 
     users.Add(user.SamAccountName); 
相关问题