.net - 覆盖成员时违反了继承安全规则 - SecurityRuleSet.Level2

标签 .net security

我有一个继承自 Exception 的类。在 .NET 4 中,我开始收到运行时错误:

Inheritance security rules violated while overriding member: MyBusinessException.GetObjectData(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.

我认为这个问题是由于我重写了 GetObjectData 而引起的。

我知道解决该问题的一个方法是设置 SecurityRuleSet:

[assembly: SecurityRules(SecurityRuleSet.Level1)]

这不是一个可接受的答案,我想知道如何解决这个问题而不必放宽 .NET 4 中的默认安全规则。

最佳答案

SecurityCriticalAttribute标记GetObjectData,因为它应用于Exception.GetObjectData。被覆盖的成员应具有相同的安全可访问性(关键、安全关键或透明)。

阅读Security Changes in the .NET Framework 4Security Transparent Code, Level 2从 MSDN 获取更多信息。

为避免所有潜在的安全运行时异常,请使用安全规则集启用代码分析。您将收到可能与运行时错误相对应的静态分析警告。

关于.net - 覆盖成员时违反了继承安全规则 - SecurityRuleSet.Level2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244308/

相关文章:

.net - Type.GetType()、HashSet<T> 和程序集限定

database - 保护数据库访问凭证的安全

java - 当经过身份验证的用户访问未 protected 页面时,getRemoteUser() 返回 null

c# - 自定义数字格式字符串 : Dynamic Decimal Point

.net - 检测 Windows 机器是否正在运行病毒扫描程序?

c# - Windows.UI.Xaml.dll 中的应用程序内部异常

c# - ModelMetadataType对asp.net core 2.0中的模型没有影响

java - 从 servlet 打开受密码保护的网站

security - 如何判断我的邮件系统是否已受到威胁?

winforms - WinForm 应用程序中的安全连接字符串