首先,我通常是编程的新手。我正在使用一个简单的监视工具。
我正在尝试获取所有锁定广告用户的列表。多亏了Stackoverflow,我找到了一个曾经有同样问题的人,不幸的是,他的答案对我有用。而且我真的不知道为什么,但是我认为我正在正确搜索。
错误
(粗略翻译:值不能为null。参数名称:IdentityValue(
尝试在以下代码中搜索"域用户"的替代方案,但没有运气。
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
找到身份,因为FindIdentity
-method的第二个参数是身份类型要过滤,但您提供的是UserPrincipalName
而不是SamAccountName
。以下选项将解决您的问题:
var user = UserPrincipal.FindByIdentity(context, IdentityType.UserPrincipalName, userPrincipal.UserPrincipalName);
或:
var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, userPrincipal.SamAccountName);