用于加载其他DLL的.Net对象标记



好的,所以我有一个项目来创建一个.Net控件,该控件通过网页中的COM对象与客户端条形码扫描仪交互(只需要IE)。为此,我们使用了一个.Net Windows窗体控制库DLL,该DLL是使用Object标签加载的:

<object id="scannerCont" classid="http:ControlAssembly.dll#Controls.MyControl"/>

到目前为止,我已经获得了在网页中呈现的.Net控件,但现在我遇到了麻烦,因为我无法获得访问额外DLL的控件。基本上,控件调用一个非托管DLL,该DLL提供对扫描单元的COM互操作访问。当我加载页面而不尝试访问DLL时,它加载得很好,但当我尝试引用它时,我在临时internet文件文件夹中收到FusionBindErrors。

错误如下:

*****   IEHOST Error Log (Friday, 12 October 2012 09:27)    *****

URL:        http://scanner:8014/scanner/ControlAssembly.dll
Zone:       2
Assembly Name:  ControlAssembly.dll
Type Name:  Controls.MyControl

----- Thrown Exception -----

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileLoadException: Could not load file or assembly 'ActiveScanner, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. Failed to grant minimum permission requests. (Exception from HRESULT: 0x80131417)
File name: 'ActiveScanner, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' ---> System.Security.Policy.PolicyException: Required permissions cannot be acquired.
at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Boolean checkExecutionPermission)
at System.Security.SecurityManager.ResolvePolicy(Evidence evidence, PermissionSet reqdPset, PermissionSet optPset, PermissionSet denyPset, PermissionSet& denied, Int32& securitySpecialFlags, Boolean checkExecutionPermission)
at Controls.MyControl..ctor()
--- End of inner exception stack trace ---
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic)
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateComInstanceFrom(String assemblyName, String typeName, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
at System.AppDomain.CreateComInstanceFrom(String assemblyFile, String typeName, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
at System.AppDomain.CreateComInstanceFrom(String assemblyFile, String typeName, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm)
at Microsoft.IE.SecureFactory.CreateInstanceWithSecurity2(Int32 dwFlags, Int32 dwZone, String wszSite, String wszId, String wszConfig, String wszLicenses, String wszDeploymentManifest)

现在,据我所知,这个错误是由于权限问题造成的。在你们回复说要将网页添加到受信任的网站列表之前,这不是一个选项。这是一个面向客户的组件,我们最多只能在收到安全警告消息时提交它们,他们可以单击该消息以允许控件提升权限。

我一直在努力寻找解决方案,但这似乎不是一种被广泛采用的做法,因此,关于如何减轻这一错误的文件很少。

不过,我想注意的是,如果我从加载控件的网站文件夹中删除无关的DLL,我会收到一个File Not Found FusionBindError,这意味着一旦我解决了这个权限问题,系统应该能够加载这个无关的DLL。。。

有人有授予此权限的经验吗?同样,我们可以接受最终用户从.Net收到弹出窗口,要求他们允许授予此权限,但我们不能要求最终用户将我们的网站添加到受信任区域。

我想知道我是否可以在对象标记中添加一些信息,说明它也需要这个其他DLL?

IE中托管的.NET控件在具有受限权限的"沙盒"中运行,正如您所看到的。对于.NET 2.0/3.0/3.5,调整权限的唯一方法是修改客户端计算机上的代码访问安全性(CAS)策略。我无法从错误消息中判断出所需的特定权限,但我猜ActiveScanner.dll要求"FullTrust"(因为它必须与COM互操作),并且Internet Explorer中没有默认授予完全信任的区域。

有了.NET 4.0+,沙箱模型发生了变化(它使用代码中的属性——SecurityCrtical、SecurityTransparent等);我还没有使用过.NET 4.0,但我认为Internet Explorer不会对您的控制授予"完全信任"。

总结一下:我不知道你可以在网页/对象标签上做任何事情来请求额外的权限。IE根本不会在不修改客户端PC上的某些内容的情况下授予完全信任权限。

最新更新