当在以用户身份登录且加入该域的计算机上运行时,这会失败。
private static SecurityIdentifier GetGroupSid(string domainName, string groupName)
{
using (var d = Domain.GetDomain(new DirectoryContext(DirectoryContextType.Domain, domainName)))
{
using (var context = new PrincipalContext(ContextType.Domain, d.Name))
{
using (var group = GroupPrincipal.FindByIdentity(context, groupName))
{
return group.Sid;
}
}
}
}
[Test]
public void should_check_role_with_sid()
{
var barDomain = "bar.example.com";
var groupinBar = GetGroupSid(barDomain, "group_in_bar");
var identity = WindowsIdentity.GetCurrent();
var windowsPrincipal = new WindowsPrincipal(identity);
Assert.That(windowsPrincipal.IsInRole(groupinBar), Is.True, "Checking role " + groupinBar);
}
你是怎么指定'groupB'的?你有没有按名称指定它?还是你通过SecurityIdentifier指定了它?尝试使用SecurityIdentifier – 2012-08-07 05:12:02
不完全确定你的意思。 B组是二级域(域B)上的“目标”组。组A(来自主域)嵌套在组B中。 – user1154725 2012-08-08 16:49:01
“IsInRole”有多个版本。其中一个接受'string'作为参数。另一个接受'SecurityIdentifier'作为参数。 groupB的类型是什么? – 2012-08-08 21:52:04