我的应用程序存在权限问题:
我有一个在完全受信任的应用程序域中运行的主机应用程序。该主机通过 MAF 框架加载插件,并在另一个只有 Internet 访问权限的应用程序域中激活该插件。
主机在主应用程序域中创建一个助手对象,并通过 MAF 管道将其引用传递给加载项(使用 HostView 和加载项 View 适配器)。加载项然后调用此 Helper-Object 上的方法,该方法应从文件系统加载文本文件。执行此操作时,我遇到了 SecurityException:
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Additional information: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
我已经稍微调试了一下代码,发现在Class FileStream.cs中,有如下检查:
new FileIOPermission(secAccess, control, new String[] { filePath }, false, false).Demand();
Demand-Method 在 CodeAccessPermissions.cs 中实现,如果所有元素都有权执行此方法,它似乎会检查完整的调用堆栈:
StackCrawlMark stackMark = StackCrawlMark.LookForMyCallersCaller;
当我直接从 Main 方法中对 Helper 类执行此方法时,一切正常。
当我将加载项的权限设置为 FullTrust 时,它也能正常工作。
我还检查了 AppDomain 和 AppDomain.CurrentDomain.IsFullyTrusted 属性,这在所有情况下都是正确的。
看来是AddIn在Call-Stack中的问题,导致权限问题。
我还尝试在一个新线程中执行此操作以不再在调用堆栈中包含 AddIn,但这没有效果。
这个问题对我来说非常重要,因为我不想授予插件完全权限,而是让插件在主机上执行方法。
有人知道这个问题的解决方案吗?
最佳答案
同时我找到了一个解决方案:
可以通过在权限对象上使用断言方法来停止所谓的 Stack Walk:
PermissionSet permSet = new PermissionSet(PermissionState.Unrestricted);
permSet.Assert();
//Do the problematic Stuff
PermissionSet.RevertAssert();
使用 RevertAssert,StackWalk 不会再在这里停止。
亲切的问候
托比
关于c# - 使用两个 AppDomain 的 Microsoft AddIn Framework (MAF) 回调的安全异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30031721/