.net CodeAccessSecurityEngine异常

.net CodeAccessSecurityEngine异常,.net,exception,code-access-security,.net,Exception,Code Access Security,当我的应用程序启动时,我的两个用户(几千个用户中的两个)遇到了一个奇怪的异常。我一直无法通过谷歌、MSDN或Reflector找到任何关于它的有用信息 如果你想复制粘贴这些东西,这里写着: External component has thrown an exception. Stack trace: at System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken, CodeAcce

当我的应用程序启动时,我的两个用户(几千个用户中的两个)遇到了一个奇怪的异常。我一直无法通过谷歌、MSDN或Reflector找到任何关于它的有用信息

如果你想复制粘贴这些东西,这里写着:

External component has thrown an exception.

Stack trace:
  at System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark& stackMark, Int32 unrestrictedOverride, Int32 create)
  at System.Security.SecurityRuntime.Assert(PermissionSet permSet, StackCrawlMark& stackMark)
  at System.Security.PermissionSet.Assert()
  at System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfNeeded()
  at System.Winfows.Forms.Control..ctor(Boolean autoInstallSyncContext)
  .....
  at GrasshopperPlugin.GrasshopperPlugin.OnLoadPlugIn() //This is the first call to my assembly.
  at RhDN_NativePlugInBase<CRhinoUtilityPlugIn,RMA::Rhino::MRhinoUtilityPlugIn>.OnLoadPlugIn() //This is the C++/CLI mixed mode SDK that loads my .NET assembly.
外部组件引发了异常。
堆栈跟踪:
在System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken、CodeAccessPermition demand、StackScrawlMark和stackMark、Int32 unrestrictedOverride、Int32 create)
在System.Security.SecurityRuntime.Assert(PermissionSet permSet、StackScrawlMark和stackMark)
在System.Security.PermissionSet.Assert()中
在System.Windows.Forms.WindowsFormsSynchronizationContext.InstallIfRequired()中
位于System.Winfows.Forms.Control..ctor(布尔值autoInstallSyncContext)
.....
在GrasshopperPlugin.GrasshopperPlugin.OnLoadPlugIn()//这是对我的程序集的第一次调用。
在RhDN_NativePlugInBase.OnLoadPlugIn()//这是加载.NET程序集的C++/CLI混合模式SDK。

这是什么意思?为什么会这样?如何修复/检测此问题?

我使用ILSpy检查
WindowsFormsSynchronizationContext.InstallIfNeeded的代码

它添加了以下权限集:

new PermissionSet(PermissionState.Unrestricted).Assert();
然后调用

AsyncOperationManager.SynchronizationContext = new WindowsFormsSynchronizationContext();
在setter上具有以下属性:

[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]

如果我正确理解安全模型,您必须确保允许用户在“完全信任”模式下运行。

只是一些提示,它不是堆栈跟踪中的水晶。在CLR内部失效的代码是用C++编写的,这是您得到不完美堆栈跟踪的原因。首先要做的是查看输出窗口,您很可能会看到“First chance”异常通知。可能对于0xc0000005,访问冲突异常。然后,通过启用非托管代码调试(项目+属性,调试选项卡),启用Microsoft符号服务器(工具+选项,调试),然后勾选调试+异常中的抛出框,以便调试器在非托管异常时停止,您可以获得更多的洞察力。没有任何代码可以查看,但堆栈跟踪可能会显示一个提示

如果做不到这一点,就有助于对问题进行推理。失败的代码遍历堆栈,查找堆栈帧以验证调用该方法的代码是否具有正确的访问权限。当堆栈损坏时,此代码可能会失败。这是非常罕见的,托管代码不会损坏堆栈。问题是:并不是所有的代码都被管理。最引人注目的是RhDN_NativePlugInBase.OnLoadPlugIn()。用C++/CLI编写,因此很有可能出现堆栈缓冲区溢出之类的情况。可能有点傻,比如插件路径太长,超过259个字符。C++代码常常会被忽略。


另一个非常强烈的提示是堆栈跟踪本身,该函数会破坏堆栈。它停止得太快,没有显示谁调用了OnLoadPlugin。这几乎肯定是麻烦制造者。修复它会让人不愉快,您需要此库的源代码并调试C++/CLI代码。放弃图书馆也许应该是你的首要任务。

谢谢汉斯,信息量很大。幸运的是,C++/CLI的工作是由我的一位同事完成的,因此我们可以完全访问所有代码。问题是我们中没有人能在可调试的机器上重复这个问题。但这至少让我们开始了一个新的方向,再次感谢。是的,这肯定是环保的。在代码中查找堆栈缓冲区溢出的可能性。