搜索仅具有 SID 的 AD



有没有办法使用只有SID的UserPrincipal搜索活动目录?我在 byte[] 中有 SID(以前使用 DirectorySearcher 查询过),并使用 StringBuilder 将其转换为"S-1-15-..."和"\01\05.."。

我试图以这种方式处理它:

PrincipalContext pContext = new PrincipalContext(ContextType.Domain);
UserPrincipal pUser = new UserPrincipal(pContext);
pUser.Sid = stringBuilder.ToString();
PrincipalSearcher pSearcher = new PrincipalSearcher();
pSearcher.QueryFilter = pUser;
Console.WriteLine(pSearcher.FindOne().DistinguishedName.ToString());

Visual Studio告诉我,Sid是写保护的。答案是肯定的。。。

提前感谢和干杯亚历克斯

p.s.:我已经尝试过按照这里描述的方式解决它:如何在 C# 中从 SID 转换为帐户名称,但这里没有成功。

你当然可以。我使用以下方法在名为"Atlas"的内部应用程序中查找我的用户。请原谅格式。

using (var context = new PrincipalContext(ContextType.Domain, "DOMAIN_NAME_IMPORTANT"))
{
    var userIdentity = UserPrincipal.FindByIdentity(context, "USER GUID GOES HERE"));
}

迟到总比没有好。你的问题帮助了我,所以我想我会尝试为后代增加这个答案的完整性。请注意,我的上下文是本地计算机,FindByIdentity太慢了。

你走在正确的道路上。我发现这个答案对于使用 LINQ 指定PrincipalSearcher返回的集合中的搜索参数特别有用。这些查询使我可以通过 SID 快速找到帐户:

private static GroupPrincipal GetGroup(string accountSid)
{
  PrincipalContext oPrincipalContext = GetPrincipalContext();
  GroupPrincipal oGroupPrincipal = new GroupPrincipal(oPrincipalContext);
  return new PrincipalSearcher(oGroupPrincipal).FindAll().Cast<GroupPrincipal>()
  .FirstOrDefault(x => x.Sid.Value.Equals(accountSid, StringComparison.OrdinalIgnoreCase));
}
private static UserPrincipal GetUser(string accountSid)
{
  PrincipalContext oPrincipalContext = GetPrincipalContext();
  UserPrincipal oUserPrincipal = new UserPrincipal(oPrincipalContext);
  return new PrincipalSearcher(oUserPrincipal).FindAll().Cast<UserPrincipal>()
  .FirstOrDefault(x => x.Sid.Value.Equals(accountSid, StringComparison.OrdinalIgnoreCase));
}
private static PrincipalContext GetPrincipalContext()
{
  return new PrincipalContext(ContextType.Machine, Environment.MachineName);
}

有关更多详细信息,请参阅我关于此主题的帖子。

最新更新