我有一个 [AllowPartiallyTrustedCallers] 类库,其中包含 System.DataAnnotations.ValidationAttribute 的子类型。该库用于 WCF 服务的契约(Contract)类型。
在 .NET 2/3.5 中,这运行良好。但是,从 .NET 4.0 开始,在 Visual Studio 调试器中运行该服务的客户端会导致异常“类型违反继承安全规则:'(我的 ValidationAttribute 子类型)'。派生类型必须与基类型的安全可访问性匹配或者不太容易接近。” (System.TypeLoadException)
仅当满足以下所有条件时才会出现该错误:
所以基本上,在 Visual Studio.NET 2010 中:
.
using System;
[assembly: System.Security.AllowPartiallyTrustedCallers()]
namespace TestingVaidationAttributeSecurity
{
public class MyValidationAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
{ }
[MyValidation]
public class FooBar
{ }
class Program
{
static void Main(string[] args)
{
Console.WriteLine("ValidationAttribute IsCritical: {0}",
typeof(System.ComponentModel.DataAnnotations.ValidationAttribute).IsSecurityCritical);
FooBar fb = new FooBar();
fb.GetType().GetCustomAttributes(true);
Console.WriteLine("Press enter to end.");
Console.ReadLine();
}
}
}
按 Ctrl-F5(无需调试即可启动),一切正常,无一异常(exception)......
奇怪的是,根据您运行程序的方式(F5 或 Ctrl+F5),ValidationAttribute 是否是安全关键的。如上面代码中的 Console.WriteLine 所示。但话说回来,这似乎也发生在其他属性(和类型?)上。
现在问题...
为什么从 ValidationAttribute 继承时会出现这种行为,而从 System.Attribute 继承时却不会? (使用 Reflector 我没有在 ValidationAttribute 类或其程序集上找到特殊设置)
我该怎么做才能解决这个问题?如何在 AllowPartiallyTrustedCallers 程序集中保持从 ValidationAttribute 继承的 MyValidationAttribute 而不将其标记为 SecurityCritical,仍然使用新的 .NET 4 级别 2 安全模型,并且仍然使用 VS.NET 调试主机(或其他主机)使其工作?
非常感谢!
鲁迪
最佳答案
Why do I have this behaviour when inheriting from ValidationAttribute, but not when inheriting from System.Attribute ? (Using Reflector I don't find special settings on the ValidationAttribute class or it's assembly)
这是因为 System.ComponentModel.DataAnnotations 程序集是有条件的 APTCA,即它标记有以下属性。
[assembly: AllowPartiallyTrustedCallers(PartialTrustVisibilityLevel = PartialTrustVisibilityLevel.NotVisibleByDefault)]
Visual Studio 启动宿主进程的方式会导致 CLR 不遵守此程序集上的 APTCA,即使默认 AppDomain 是完全受信任的。这意味着 DataAnnotations 程序集中的所有类型和方法都是 SecurityCritical。由于安全透明类型 (MyValidationAttribute) 不能从安全关键类型 (ValidationAttribute) 继承,因此会引发此异常。
And what can I do to solve this ? How can I keep MyValidationAttribute inheriting from ValidationAttribute in an AllowPartiallyTrustedCallers assembly without marking it SecurityCritical, still using the new .NET 4 level 2 security model and still have it work using the VS.NET debug host (or other hosts) ??
这似乎是VS主机的一个错误,这对您的情况来说很不幸。另一方面,您真的应该确定您希望您的程序集是 APTCA。如果有必要,那么您有几个选择。
关于security - 如何从 DataAnnotations.ValidationAttribute 继承(它在 .NET 4 中的 Visual Studio 调试主机下出现 SecureCritical !),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2690291/