获取锁定的 AD 用户列表会引发错误。我使用的搜索词不正确吗?

Fetching list of locked AD users throws error. Am I using incorrect search terms?

首先,总的来说,我对编程还很陌生。我正在开发一个简单的监控工具。

我正在尝试获取所有锁定的 AD 用户的列表。感谢 Whosebug,我找到了曾经有过同样问题的人,不幸的是他的回答对我没有用。我真的不明白为什么,但我想我的搜索是正确的。下面的代码会抛出以下错误。

Error

(大致翻译:值不能为空。参数名称:IdentityValue)

尝试在下面的代码中搜索 "Domain Users" 的替代方案,但没有成功。

GroupPrincipal grp = GroupPrincipal.FindByIdentity(context, 
IdentityType.SamAccountName, "Domain Users");

这是我正在使用的代码。

var lockedUsers = new List<UserPrincipal>();
            using (var context = new PrincipalContext(ContextType.Domain, 
"domainname"))
            {
                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);
                        }
                    }
                }
            }

我能够重现这个问题,错误出现在以下行中:var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userPrincipal.UserPrincipalName); 您正在尝试通过 SamAccountName 查找身份,因为 [=12] 的第二个参数=]-method 是要过滤的身份类型,但您提供的是 UserPrincipalName 而不是 SamAccountName。以下选项可以解决您的问题:

var user = UserPrincipal.FindByIdentity(context, IdentityType.UserPrincipalName, userPrincipal.UserPrincipalName);

或:

var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userPrincipal.SamAccountName);