为什么UserPrincipal.Enabled返回不同的值?

时间:2013-05-15 02:47:28

标签: c# active-directory

我正在尝试确定是否启用了AD中的用户帐户。为此,我使用以下代码:

string domain = "my domain";
string group = "my security group";
string ou = "my OU";

//init context
using (var cnt= new PrincipalContext(ContextType.Domain, domain))
{
    //find the necessary security group
    using (GroupPrincipal mainGroup 
              = GroupPrincipal.FindByIdentity(cnt, IdentityType.Guid, group))
    {
        if (mainGroup != null)
        {
            //get the group's members
            foreach (var user in mainGroup.GetMembers()
                                   .OfType<UserPrincipal>()
                                   .Where(u => u.DistinguishedName.Contains(ou)))
            {
                //ensure that all the info about the account is loaded
                //by using FindByIdentity as opposed to GetMembers
                var tmpUser= UserPrincipal.FindByIdentity(cnt, 
                                                          user.SamAccountName);
                //actually I could use `user` variable, 
                //as it gave the same result as `tmpUser`.

                //print the account info
                Console.WriteLine(tmpUser.Name + "\t" + 
                                  tmpUser.Enabled.HasValue + "\t" + 
                                  tmpUser.Enabled.Value);                    
            }
        }
    }
}

问题是,当我在管理帐户下运行此代码时,我得到了真实的结果,而当我在非特权帐户下运行时,user.Enabled会返回false的某些帐户,但应该是true

我设法找到的唯一类似q&amp; a

  1. UserPrincipal.Enabled returns False for accounts that are in fact enabled?
  2. Everything in Active Directory via C#.NET 3.5 (Using System.DirectoryServices.AccountManagement)
  3. 在这里没有帮助。

    为什么会这样?我可以选择在非特权帐户下获取此信息吗?


    这是另一种方法:How to determine if user account is enabled or disabled

    private bool IsActive(DirectoryEntry de)
    {
        if (de.NativeGuid == null) 
            return false;
    
        int flags = (int)de.Properties["userAccountControl"].Value;
    
        if (!Convert.ToBoolean(flags & 0x0002)) 
            return true; 
        else 
            return false;
    }
    

    Active Directory Objects and C#中描述了相同的方法。

    但是,如果在无特权的用户帐户下运行,userAccountControl属性为null,则无法确定帐户的状态。


    此处的解决方法是使用PrincipalContext Constructor,指定具有足够权限的用户的凭据以访问AD。

    我不清楚,为什么没有特权的用户完全可以访问AD,并且无法获得某些特定帐户属性的值。可能这与C#无关,应该在AD中配置......

1 个答案:

答案 0 :(得分:1)

您需要在Active Directory中为将要执行AD查询的帐户委派权限。这是我必须为我的应用程序工作所做的事情(尽管我们正在对用户帐户执行其他管理任务)。

检查Here以获取有关如何委派权限的说明(或参见下面的blockquote)。

  

您可以参考以下过程来运行委派:

     
      
  • 执行以下步骤,启动控制向导委派:      
        
    • 打开Active Directory用户和计算机。
    •   
    • 在控制台树中,双击域节点。
    •   
    • 在详细信息菜单中,右键单击组织单位,单击委托控制,然后单击“下一步”。
    •   
    • 选择要委派常见管理任务的用户或组。为此,请执行以下步骤:
    •   
    • 在“用户或组”页面上,单击“添加”。
    •   
    • 在选择用户,计算机或组中,写下您必须委派组织单位控制权的用户和组的名称,然后单击“确定”。然后单击“下一步”。
    •   
    • 将常用任务分配给委派。为此,请执行以下常见任务。
    •   
    • 在delgate页面的任务中,单击委派以下常见任务。
    •   
    • 在要委派的任务页面上,选择要委派的任务,然后单击“确定”。单击“完成”
    •   
  •   
     

例如:要委派管理员移动用户/计算机对象,可以在AD用户和计算机中使用高级模式并运行委派。对于移动的对象,它应该在两个OU中都具有写权限。为了编写新值,管理员帐户应该在用户帐户上具有委派值(特定OU中的完全权限。

其他值得研究的是帐户是否具有userAccountControl属性。我听说缺少此属性的帐户可能无法正确报告。在大多数情况下,此属性应设置为NormalAccount。