在您的特定情况下,最快 的开始方式是添加 SecurityRules 属性,以便您获得旧行为,但我不确定我是否会考虑 正确的方式.正确的方法可能是丢失 APTCA 并在程序集上添加 SecurityCritical 因为程序集可能包含 SecurityCritical 代码,然后用 SecuritySafeCritical 标记调用 SecurityCritical 代码的各种类型(例如,引用 GetObjectData 的内容),以便您的 SecurityTransparent 代码可以调用它.当然,第二种方法需要做更多的工作,因此您可能希望运行 SecAnnotate.exe 并获得一些自动提示.查看 Moq 主干,搜索 GetObjectData 显示有问题的方法是异常序列化机制的覆盖(System.Exception 上的 ISerializable.GetObjectData),无论如何只有 SecurityCritical 代码会调用它,因此您可能不会如果您只是丢失 APTCA 并标记程序集 SecurityCritical,甚至会遇到任何麻烦.Autofac 提出了一个问题,需要将其更新为最新的安全模型. 如果你喜欢这个想法,去投票/评论它.抱歉,这不是一个简短的答案.不幸的是,安全从来都不是一件容易的事.:SI'm new C# and am trying to understand the new security features of .NET-4.To fill in some details, I'm currently trying to update AutofacContrib.Moq to work with the latest Moq. I had no problems doing this for .NET-3.5 and under. But in .NET-4 the security restrictions result in numerous security exceptions.Moq has a a single method, GetObjectData, that's marked with the SecurityCritical attribute. AutofacContrib.Moq has the AllowPartiallyTrustedCallers attribute set which is the source of the exceptions. It seems that rather than adding the SecurityRules attribute with a SecurityLevel of 1, I'd be better off removing AllowPartiallyTrustedCallers attribute. I believe this makes the assembly SecurityTransparent by default, which may not be sufficient (though the AutofacContrib.Moq unit tests pass).My main question at the moment is whether assemblies targeting .NET-4 should ever use the AllowPartiallyTrustedCallers attribute? But, given that I definitely don't understand everything yet, what details should be considered when working with assemblies that are security marked? Do I need to explicitly mark my assembly with security attributes in those places it uses, directly or indirectly, something that's marked SecurityCritical? 解决方案 You are correct: in .NET 4, leaving the APTCA on there makes the assembly SecurityTransparent, and that may be what's causing you grief.The MSDN article Migrating an APTCA Assembly to the .NET Framework 4 has a good discussion and explanation of the changes to the AllowPartiallyTrustedCallersAttribute in .NET 4.Specifically:And...(It's really a good article that author Mike Rousos did a great job with. I encourage you to read it in its entirety.)If you're starting a new .NET 4 library, it's probably best to stick with the .NET 4 security model and use the appropriate SecurityCritical, SecuritySafeCritical, and SecurityTransparent attributes where needed. They're far easier to manage and understand than old code access security.If you're migrating an old library to the new model, there's a good example in the article of how to do that... but basically it amounts to removing old LinkDemands and adding [SecurityCritical] in their place.In your particular case, the fastest way to get going would be to add the SecurityRules attribute so you get the old behavior, but I'm not sure I'd consider that the right way. The right way would probably be to lose the APTCA and add SecurityCritical on the assembly because the assembly may contain SecurityCritical code, then mark the various types that call SecurityCritical code (e.g., stuff that references GetObjectData) with SecuritySafeCritical so your SecurityTransparent code can call it. Of course, that second approach will be a lot more work, so you'll probably want to run SecAnnotate.exe and get some automated tips.Looking at the Moq trunk, a search for GetObjectData shows that the method in question is the override for an exception serialization mechanism (ISerializable.GetObjectData on System.Exception), which only SecurityCritical code will be calling anyway, so you may not even run into any trouble if you just lose APTCA and mark the assembly SecurityCritical.There is an issue filed on Autofac to update it to the latest security model. If you like the idea, go vote/comment on it.Sorry that wasn't a short answer. Security is, unfortunately, never easy. :S 这篇关于.NET 4、AllowPartiallyTrustedCallers 属性和 SecurityCritical 等安全标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!