我有一个使用 PrincipalPermission 类的 vb.net 3.5 应用程序来确保用户是角色的成员。该代码适用于 Active Directory 域中的某些组,但不适用于其他组。起初我认为空间是一个问题,但我检查了“域用户”,它有效。运行此代码我是 App Group 的成员。
Imports System.Security
Imports System.Security.Principal
Imports System.Security.Permissions
Private Function DemandSecurity() As Boolean
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal)
Dim principalGroup As New PrincipalPermission(Nothing, "App Group")
Try
principalGroup.Demand()
Debug.Print("Demanding pricipal permissions for current user on 'App Group' role succeeded. ")
Catch secEx As SecurityException
Debug.Print("Security Exception - Demanding pricipal permissions for current user on 'App Group' role failed. ")
Application.DoEvents()
MessageBox.Show("Permission denied. Output: " & vbNewLine & secEx.ToString, "App - Security Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Return False
Exit Function
End Try
Return True
End Function
secEx.ToString 的错误输出是
“System.Security.SecurityException:对主体权限的请求失败。在 System.Security.Permissions.PrincipalPermission.Demand() 的 System.Security.Permissions.PrincipalPermission.ThrowSecurityException() 中的 App.My.MyApplication.DemandSecurity() 中: \Documents and Settings\me\My Documents\Visual Studio 2008\Projects\App\App\ApplicationEvents.vb:line 28
失败的操作是:Demand 失败的第一个权限的类型是:System.Security.Permissions.PrincipalPermission
第一个失败的权限是:IPermission class="System.Security.Permissions.PrincipalPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"> Identity Authenticated="true" Role="应用组"/>
需求是:IPermission class="System.Security.Permissions.PrincipalPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1"> Identity Authenticated="true" Role="App Group "/>
失败的程序集或 AppDomain 是:mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"
让我知道是否需要包含其他内容。