c# - 使用c#查找AD中所有被锁定的用户

标签 c# active-directory

下面列出的代码对我来说工作正常但是我想要一个所有被锁定的用户的列表而不是指定一个特定的用户,请有人帮助我扩大这个代码的范围

using (var context = new PrincipalContext( ContextType.Domain ))
{
     using (var user = UserPrincipal.FindByIdentity( context,
                                                     IdentityType.SamAccountName,
                                                     name ))
     {
          if (user.IsAccountLockedOut())
          {
              ... your code here...
          }
     }
}

上面列出的代码对我来说工作正常,但是我想要一个所有被锁定的用户的列表,而不是指定一个特定的用户。

这是最终起作用的 - 感谢所有贡献者。
当用户被锁定时,他们在登录之前不会进入“未锁定”状态(在 ldap 搜索中引用 lockedout 子句时);所以... 使用锁定的 qry 为您提供了一个广泛的锁定用户列表,然后您可以使用 isaccountlockedout() 方法缩小范围。问候!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.AccountManagement;

namespace ConsoleApplication5
{
    class Lockout : IDisposable
    {
        DirectoryContext context;
        DirectoryEntry root;


        public Lockout()
        {
            string domainName = "domain.com";
            this.context = new DirectoryContext(
              DirectoryContextType.Domain,
              domainName
              );

            //get our current domain policy
            Domain domain = Domain.GetDomain(this.context);

            this.root = domain.GetDirectoryEntry();

        }

        public void FindLockedAccounts()
        {

            string qry = " (&(&(&(objectCategory=person)(objectClass=user)(lockoutTime:1.2.840.113556.1.4.804:=4294967295)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(!userAccountControl:1.2.840.113556.1.4.803:=65536)))) ";
            DirectorySearcher ds = new DirectorySearcher(
              this.root,
              qry
              );

            using (SearchResultCollection src = ds.FindAll())
            {
                foreach (SearchResult sr in src)
                {


                    using (var context = new PrincipalContext( ContextType.Domain ))
                        {
    string name = sr.Properties["SamAccountName"][0].ToString();
     using (var user = UserPrincipal.FindByIdentity( context,
                                                     IdentityType.SamAccountName,
                                                     name ))
                                 {  
          if (user.IsAccountLockedOut())
                                                 {
              Console.WriteLine("{0} is locked out", sr.Properties["name"][0]);

                                                 } 
                                 }
                        }



                }
            }
        }

        public void Dispose()
        {
            if (this.root != null)
            {
                this.root.Dispose();
            }
        }
    }


}

最佳答案

编辑:误读问题。更新的答案

立即尝试

为什么不只是:

        var lockedUsers = new List<UserPrincipal>();
        using (var context = new PrincipalContext(ContextType.Domain))
        {
            GroupPrincipal grp = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "Domain Users");
            foreach (var userPrincipal in grp.GetMembers(false))
            {
                var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userPrincipal.UserPrincipalName);                        
                if (user != null)
                {
                    if (user.IsAccountLockedOut())
                    {
                        lockedUsers.Add(user);
                    }
                }
            }
        }
//Deal with list here

Check here if you'd like to see more

关于c# - 使用c#查找AD中所有被锁定的用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25655035/

相关文章:

c# - OpenFileDialog() 锁定文件夹

c# - Windows 商店应用程序中的 TypeLoadException

c# - Watin 不会手动触发 .change() 事件

c# - 获取 2 个字符之间的字符串正则表达式

c# - ActiveDirectoryMembershipProvider 验证用户

java - 如何针对 Java 中的服务器验证 Kerberos 票证?

python - LDAP 响应元组 (97, []) 是什么意思?

c# - 防止使用先前输入的值自动填充文本框

coldfusion - 如何判断用户是否属于事件目录中的角色 - 使用 ColdFusion

c# - 如何扩展泛型?