DaF*_*lex 2 c# active-directory
有没有办法使用仅具有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());
Run Code Online (Sandbox Code Playgroud)
Visual Studio告诉我,Sid是写保护的.当然...
在此先感谢和干杯亚历克斯
ps:我已经尝试按照这里描述的方式解决它:如何在S#中将SID转换为帐户名,但这里没有成功.
你当然可以.我使用以下方法在名为"Atlas"的内部应用程序中查找我的用户.请原谅格式化.
using (var context = new PrincipalContext(ContextType.Domain, "DOMAIN_NAME_IMPORTANT"))
{
var userIdentity = UserPrincipal.FindByIdentity(context, "USER GUID GOES HERE"));
}
Run Code Online (Sandbox Code Playgroud)
迟到总比不到好。你的问题帮助了我,所以我想我会尝试为后代增加这个答案的完整性。请注意,我的上下文是本地机器,这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);
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅我关于此主题的帖子。
| 归档时间: |
|
| 查看次数: |
3699 次 |
| 最近记录: |