2011-03-23 37 views
0

我有一個嵌入在HTML頁面(在IE7中)的.Net 2.0 ActiveX控件。 我使用javascript來修改其屬性和調用方法。在我調用一個使用Activator.CreateInstance(type)實例化類的方法之前,這一切都很好。我收到以下消息:實例化Intranet區域中的類時的安全問題.Net

System.Reflection.TargetInvocationException:調用的目標引發了異常。 ---> System.Security.SecurityException:請求失敗。
..
..
失敗了行動:InheritanceDemand
失敗的類型的第一個權限是:System.Security.PermissionSet
大會的失敗區域爲:內網

我嘗試實例化的類有一個無參數的構造函數public,並且從我讀過的內容來看,在公共類型上使用反射應該沒有問題?

我已經通過使用Microsoft .NET Framework配置實用程序將Intranet信任修改爲已完成臨時修復。見here

如何修改方法,類或程序集以避免必須配置框架?

幾個加分:

  • ActiveX控件編譯 進行鍼對.NET 2
  • 它裝配不強命名
  • 我不打擾有關授予反射權限。

感謝

更新

事實證明,這不是反映這是造成問題的原因,這是哪個扔了FileIOPermission的安全異常TypeDescriptor.GetAttributes通話。我用下面的代碼解決了這個問題:

Dim temp As New Security.Permissions.FileIOPermission(Security.Permissions.PermissionState.Unrestricted) 
temp.Assert() 
// Get attributes 
System.Security.CodeAccessPermission.RevertAssert() 

現在,如果我設置了分配給我的程序集的強名稱的代碼組,並設置設置爲FullTrust許可,一切都很好。
但是,我似乎無法對其進行微調,它可能是FullTrust或引發異常(請參見下文)。即使所有權限集都不起作用。

例外:

System.Security.SecurityException: Request failed. 
at System.Reflection.CustomAttribute._CreateCaObject(Void* pModule, Void* pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs) 
at System.Reflection.CustomAttribute.CreateCaObject(Module module, RuntimeMethodHandle ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs) 
at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean mustBeInheritable, IList derivedAttributes) 
at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit) 
at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit) 
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectGetAttributes(Type type) 
at System.ComponentModel.ReflectTypeDescriptionProvider.ReflectedTypeData.GetAttributes() 
at System.ComponentModel.TypeDescriptor.TypeDescriptionNode.DefaultTypeDescriptor.System.ComponentModel.ICustomTypeDescriptor.GetAttributes() 
at System.ComponentModel.TypeDescriptor.GetAttributes(Object component, Boolean noCustomTypeDesc) 
at System.ComponentModel.TypeDescriptor.GetAttributes(Object component) 
... GetAttributes 
... 
The action that failed was: InheritanceDemand 
The type of the first permission that failed was: System.Security.PermissionSet 
The Zone of the assembly that failed was: Intranet 

回答

1

我試圖實例 類有一個PARM少公共構造, 從我讀過,應該 是使用上 類型是公共反正反射沒有問題?

如果類和構造函數都是公共的,那麼通過反射調用構造函數時應該沒有問題。然而,非公開課堂上的公共構造者仍然會造成問題。

這就是說,因爲它是一個繼承要求失敗,這聽起來像實際問題可能在其他地方。如果您嘗試在不使用反射的情況下從控制代碼創建類的新實例,會發生什麼?

+0

有趣。你的懷疑是正確的,一個正常的實例化也失敗了。這是對TypeDescriptor.GetAttributes的調用,它引發了FileIOPermission安全異常。我會進一步深入... – Jules 2011-03-24 20:09:36

+0

我已經添加了更新到我的帖子,以顯示我到目前爲止。 – Jules 2011-03-24 21:13:53

+1

這聽起來像你正在運行到類似於在http://weblogs.asp.net/whaggard/archive/2008/01/14/issues-with-the-xmlserializer-in-medium-trust-描述的問題environments.aspx。如果您不確定哪個屬性導致問題,您可能需要查看Reflector(http://www.reflector.net/)中的類型定義。 – 2011-03-25 14:43:34

2

我的性ReflectionPermission屬性添加到AssemblyInfo.cs文件的彙編試圖反映到另一個類,具有RequireMinimum SecurityAction。

但是,要知道,這一切都將做的是阻止應用程序在Intranet區域中運行,而不是運行一段時間,一切似乎都很好,直到反射發生。聲明權限並不意味着他們將被授予權限,它只是允許程序「快速失敗」。你可以要求許可; CAS的全部基礎是它不必授予你。

爲了在您的應用程序或程序集中使用反射,您必須提供足夠的證據在限制較少的區域運行程序集(例如,通過強制對它進行簽名),或者配置框架以將ReflectionPermission包含在Intranet權限集。

最後,要知道,聲明CAS的安全模型是在.NET Framework 4.0中棄用大部分;如果您在以後嘗試將此代碼遷移到.NET 4.0,則必須更改您聲明權限的方式。

+0

感謝您的答覆。如果我強烈地簽署大會,是嗎,還是需要進一步修改?例如,我是否需要配置框架以完全信任我簽名的程序集?這對我來說都是新的順便說一句。 – Jules 2011-03-23 20:27:27

+1

好吧,如果你用微軟強名或ECMA的強名稱以外的任何其他名稱強烈簽名,默認情況下它仍將位於Intranet區域。你所要做的是無論是裝配保存到本地計算機並從那裏運行它(因此它來自我的電腦區域並獲得完全信任),或定義自定義權限設置爲所有計算機,用標準指出代碼簽署與貴公司的強名稱落入該區域並獲得該權限。 – KeithS 2011-03-23 20:55:10