我试图获得与IsInRole类似的结果,如下例所示:
Dim MyPrincipal As New System.Security.Principal.WindowsPrincipal(New System.Security.Principal.WindowsIdentity(TextBox2.Text))
If MyPrincipal.IsInRole("DOMAIN\GroupName")
Then MsgBox("User is Member of AD Group")
End If
但我需要为群组做这件事,在这种情况下,MyPrincipal.IsInRole不起作用(我认为这只适用于用户帐户)并且之前从未尝试过群组,所以甚至不知道从哪里开始。 有人可以帮忙吗?
答案 0 :(得分:0)
已经找到答案,如果有兴趣,可以通过使用GroupPrincipal类来实现:
https://msdn.microsoft.com/en-us/library/bb339975(v=vs.110).aspx
示例:
**
Dim ctx As New PrincipalContext(System.DirectoryServices.AccountManagement.ContextType.Domain, "fabrikam.com", "DC=fabrikam,DC=com", "administrator", "SecretPwd123")
Dim grp As GroupPrincipal = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, "Administrators")
If grp IsNot Nothing Then
For Each p As Principal In grp.GetMembers(False)
If TypeOf p Is GroupPrincipal Then
Console.WriteLine(p.Name)
End If
Next
grp.Dispose()
End If
ctx.Dispose()
**
非常感谢保罗在此!