0

我有一个嵌入在 HTML 页面(在 IE7 中)中的 .Net 2.0 activex 控件。我使用javascript来修改它的属性和调用方法。在我调用使用 Activator.CreateInstance(type) 实例化类的方法之前,这一切都很好。我收到以下消息:

System.Reflection.TargetInvocationException:调用的目标已引发异常。---> System.Security.SecurityException:请求失败。
..
..
失败的操作是:InheritanceDemand
第一个失败的权限类型是:System.Security.PermissionSet 失败
的程序集区域是:Intranet

我试图实例化的类有一个无参数的公共构造函数,从我读过的内容来看,对公共类型使用反射应该没有问题吗?

我使用 Microsoft .NET Framework 配置实用程序进行了临时修复,将 Intranet 信任修改为完全。见这里

如何修改方法、类或程序集以避免必须配置框架?

一些额外的点:

  • activex 控件是针对 .Net 2 编译的
  • 它的组件不是强命名的
  • 我不介意授予反射权限。

谢谢

更新

事实证明,导致问题的不是反射,而是对 TypeDescriptor.GetAttributes 的调用引发了 FileIOPermission 安全异常。我已经用以下代码解决了这个问题:

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

现在,如果我设置一个分配给我的程序集强名称的代码组并将权限集设置为FullTrust,一切都很好。
但是,我似乎无法对其进行微调,它要么是FullTrust ,要么是抛出异常(见下文)。甚至Everything权限集也不起作用。

例外:

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
4

2 回答 2

2

我会将 ReflectionPermission 属性添加到您的 AssemblyInfo.cs 文件中,以便程序集尝试反射到另一个类中,并使用 RequireMinimum SecurityAction。

但是,请注意,所有这一切都会阻止您的应用程序在 Intranet 区域中运行,而不是运行一段时间,一切似乎都很好,直到发生反射。断言权限并不意味着他们会被授予,它只是允许程序“快速失败”。您可以随心所欲地要求许可;CAS 的全部基础是它不必授予您。

为了在您的应用程序或程序集中使用反射,您必须提供足够的证据以在限制较少的区域中运行程序集(例如,通过对其进行强签名),或者将框架配置为在 Intranet 权限集中包含 ReflectionPermission。

最后,请注意,声明式 CAS 安全模型在 .NET Framework 4.0 中已基本弃用;如果您稍后尝试将此代码迁移到 .NET 4.0,您将不得不更改您声明权限的方式。

于 2011-03-23T20:14:10.403 回答
1

我试图实例化的类有一个无参数的公共构造函数,从我读过的内容来看,对公共类型使用反射应该没有问题吗?

如果类和构造函数都是公共的,那么通过反射调用构造函数应该没有问题。但是,非公共类上的公共构造函数仍然会带来问题。

也就是说,鉴于这是一个失败的继承需求,听起来实际问题可能出在其他地方。如果您尝试在不使用反射的情况下从控制代码创建类的新实例,会发生什么情况?

于 2011-03-24T13:05:37.403 回答