UserPrincipal.FindByIdentity返回多个

时间:2019-05-22 15:53:17

标签: wpf active-directory userprincipal

我需要使用Active Directory验证用户并检查其组。问题是用户正在返回多个UserPrincipal。管理员无法找到与此用户有关的问题。我的代码简单明了;

var usr = UserPrincipal.FindByIdentity(context, username);

现在我知道我可以做到;

var usr = new WindowsPrincipal(WindowsIdentity.GetCurrent());
if (usr.IsInRole("MyRole")
{
   //Do stuff
}

这对于当前登录的用户确实有效,但是,有时候我需要对当前未登录的用户进行身份验证

1 个答案:

答案 0 :(得分:0)

我有一个使用以下命令监视最近帐户锁定的系统:

UserPrincipal.FindByLockoutTime(...)

我用它来给我列出最近锁定的帐户。 我可以使用以下名称单击用户名以深入了解详细信息:

UserPrincipal.FindByIdentity(context, userId)

我可以看到我有一个名为Administrator的帐户,每次单击该帐户进行追溯时,都会遇到相同的错误:

  

MultipleMatchesException

我发现FindByIdentity方法接受身份类型为int的重载:

https://docs.microsoft.com/en-us/dotnet/api/system.directoryservices.accountmanagement.identitytype?view=netframework-4.8

据我所知,我正在通过SamAccountName查询,将方法签名更改为:

UserPrincipal.FindByIdentity(context, 0, userId)

为我解决了这个问题。

相关问题