我做了一些示例代码,按照 Mike 的建议检查组的 SID。您只需要将组的 SID 放入SecurityIdentifier
类的构造函数中,即可针对当前登录的用户进行检查。
Private Sub DoCheck()
Dim sid As New Security.Principal.SecurityIdentifier("S-0-0-00-0000000000-0000000000-0000000000-000"),
result As Boolean
result = IsUserInGroup(sid)
End Sub
Public Shared Function IsUserInGroup(sid As Security.Principal.SecurityIdentifier) As Boolean
Dim user As UserPrincipal
user = UserPrincipal.Current
For Each group As Principal In user.GetGroups()
If group.Sid.Equals(sid) Then Return True
Next
Return False
End Function
要使代码工作,您需要导入System.DirectoryServices.AccountManagement
:
Imports System.DirectoryServices.AccountManagement
这个命名空间位于微软的System.DirectoryServices.AccountManagement.dll
,我相信它从 .Net 4.0 开始可用。