【问题标题】:Active Directory LDAP - Lock User AccountActive Directory LDAP - 锁定用户帐户
【发布时间】:2010-05-19 21:26:24
【问题描述】:

使用 System.DirectoryServices.AccountManagement 锁定 Active Directory 用户对象的最佳方法是什么?我能够确定一个帐户是否被锁定使用..

UserPrincipal principal = new UserPrincipal(context);
bool locked = principal.IsAccountLockedOut();

如何锁定帐户?有没有其他方法可以做这样的事情......

UserPrincipal principal = new UserPrincipal(context);
DirectoryEntry entry = (DirectoryEntry)principal.GetUnderlyingObject();

int val = (int)entry.Properties["userAccountControl"].Value;

entry.Properties["userAccountControl"].Value = val | 0x0010;
entry.CommitChanges();

【问题讨论】:

标签: c# .net asp.net active-directory ldap


【解决方案1】:

lock 属性根据定义是只读的,原因如下:

此属性的定义类似于:“多次提供无效密码时自动锁定用户帐户”(多少次?我猜这是在 GPO 中设置的)

给开发者一种改变这个属性的方法会和上面的定义冲突……所以你不应该设置这个值,我认为AD安全机制会阻止你这样做。

但是,您可以启用\禁用我认为更接近您想要的用户。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    CodeProject's Everything AD article has some sample code on unlocking an account。我不确定这是否能满足您的需求。

    public void Unlock(string userDn)
    {
        try
        {
            DirectoryEntry uEntry = new DirectoryEntry(userDn);
            uEntry.Properties["LockOutTime"].Value = 0; //unlock account
    
            uEntry.CommitChanges(); //may not be needed but adding it anyways
    
            uEntry.Close();
        }
        catch (System.DirectoryServices.DirectoryServicesCOMException E)
        {
            //DoSomethingWith --> E.Message.ToString();
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      此代码将用于在 AD 中锁定用户

      /// /// Locks a user account /// /// The name of the user whose account you want to unlock /// /// This actually trys to log the user in with a wrong password. /// This in turn will lock the user out /// public void LockAccount(string userName) { DirectoryEntry user = GetUser(userName); string path = user.Path; string badPassword = "SomeBadPassword"; int maxLoginAttempts = 10; for (int i = 0; i &lt maxLoginAttempts; i++) { try { new DirectoryEntry(path, userName, badPassword).RefreshCache(); } catch (Exception e) { } } user.Close(); }

      【讨论】:

        【解决方案4】:
        【解决方案5】:

        使用 userflag 属性我们可以得到用户锁定状态这是我的答案

        entryPC是DirectoryEntry的对象,这里我们传入活动目录的入口路径

         public bool IsLocked(DirectoryEntry entryPC)
            {
                if (entryPC.NativeGuid == null)
                {
                    return false;
                }
        
                int flags = (int)entryPC.Properties["UserFlags"].Value;
                bool check = Convert.ToBoolean(flags & 0x0010);
                if (Convert.ToBoolean(flags & 0x0010))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-11-26
          • 2012-08-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多