2

有没有办法使用只有 SID 的 UserPrincipal 来搜索 Active Directory?我有 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 是写保护的。当然...

提前感谢和干杯亚历克斯

ps:我已经尝试按照此处描述的方式解决它:如何将 SID 转换为 C# 中的帐户名,但这里没有成功。

4

2 回答 2

6

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

using (var context = new PrincipalContext(ContextType.Domain, "DOMAIN_NAME_IMPORTANT"))
{
    var userIdentity = UserPrincipal.FindByIdentity(context, "USER GUID GOES HERE"));
}
于 2013-09-17T14:30:43.903 回答
5

迟到总比不到好。你的问题帮助了我,所以我想我会尝试为后代增加这个答案的完整性。请注意,我的上下文是本地机器,它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);
}

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

于 2016-11-22T06:13:54.427 回答