0

我将 VS2012 与 VB.NET 一起用于 winfowms 应用程序,使用 Active Directory 角色。以没有权限的用户身份运行程序,尝试启动此表单时出现(预期的)安全异常。

我有一个看起来像这样的表格:

<PrincipalPermission(SecurityAction.Demand, Role:=Security.Roles.ADMINISTRATORS)> _
<PrincipalPermission(SecurityAction.Demand, Role:=Security.Roles.CORRECTIVE_ACTION_EDITORS)> _
<PrincipalPermission(SecurityAction.Demand, Role:=Security.Roles.GRIEVANCE_EDITORS)> _
<PrincipalPermission(SecurityAction.Demand, Role:=Security.Roles.ABOLISHMENT_EDITORS)> _
Public Class EmployeeInformationForm
...
End Class

对代码的调用如下所示:

    Private Sub SendEmployeeIDToEmployeeInformationForm(ByVal ID_in As String, ByVal employeeRecord_in As String)
    ...
        If Not formFound Then
            ' Create a new instance of the child form.
            Dim ChildForm As New EmployeeInformationForm(ID_in, employeeRecord_in) ' ** throws expected security exception here**
            Try
                ' Make it a child of this MDI form before showing it.
                ChildForm.MdiParent = Me.MdiParent
        ...
                ChildForm.Show()
            Catch ex As Exception
                ChildForm.Close()
                Throw
            End Try
        End If

在 15 或 16 次尝试后(或者变量可能是“大约 1 分钟后”?)程序崩溃。 更新:在任何类型的更多输入后,程序崩溃。我已经以没有权限的用户身份调试了代码,并且能够捕获抛出的异常 - 显然是从无处获得的。说“调用堆栈仅包含外部代码”很奇怪,并显示以下内容:

This thread is stopped with only external code frames on the call stack. External code frames are typically from framework code but can also include other optimized modules which are loaded in the target process.

Call stack with external code

mscorlib.dll!System.Security.Permissions.PrincipalPermission.ThrowSecurityException()
mscorlib.dll!System.Security.Permissions.PrincipalPermission.Demand()
mscorlib.dll!System.Security.PermissionSet.DemandNonCAS()
[Native to Managed Transition]
[Managed to Native Transition]
OHRC Database.exe!OHRC_Database.EmployeeInformationForm.Dispose(Boolean disposing)
System.dll!System.ComponentModel.Component.Finalize()

这似乎暗示它很难关闭表格?谁能告诉我为什么会抛出这个异常?

4

1 回答 1

2

异常是从终结线程抛出的(异常堆栈跟踪中的 Finalize() 调用是对此的提示),并且该线程上的用户身份也没有正确的权限。请参阅http://msmvps.com/blogs/calinoiu/archive/2006/01/07/why-is-my-application-coughing-up-a-securityexception-after-my-code-stops-running.aspx了解更多信息细节和修复。

HTH,妮可

于 2012-10-18T01:35:25.720 回答