0

我正在使用Active Directory目錄將VS2012與VB.NET用於winfowms應用程序。 以沒有權限的用戶身份運行程序,試圖啓動此表單時出現(預期的)安全異常。僅限於外部代碼的.NET安全異常

我有一個看起來像這樣的形式:

<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分鐘「?)程序崩潰。 更新:更多的任何種類的輸入後,程序崩潰。我有debugged the code as the user沒有權限,並能夠捕獲異常被拋出 - 顯然是從無處。這是很奇怪的說:「調用堆棧只包含外部代碼」,並顯示以下內容:

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() 

這似乎暗示它有一個很難關閉窗體?誰能告訴我爲什麼它拋出這個異常?

回答

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, 妮可

+0

這是*垃圾收集*拋出異常......這解釋了很多! – Watki02

+0

它是Finalize()*和* Dispose()需要重寫還是隻有一個? ...如果一個,哪一個? – Watki02

+1

正在調用的終結器在'System.ComponentModel.Component'上聲明。你不需要重寫它,因爲它似乎已經在執行了。似乎需要變通辦法屬性的唯一方法是'EmployeeInformationForm.Dispose(布爾處置)'。 –