首页 > 解决方案 > 如何在 .NET sdk 5 上实现 SecurityPermission?

问题描述

在以前版本的 .NET sdk 中,我们实现了以下代码:

[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
protected HttpResponseException(SerializationInfo info, StreamingContext context)
    : base(info, context)
{
    ErrorId = (Guid)info.GetValue("ErrorId", typeof(Guid));
    StatusCode = (HttpStatusCode)info.GetValue("StatusCode", typeof(HttpStatusCode));
    ShouldLog = info.GetBoolean("ShouldLog");
}

我已经迁移到 .NET 5,我们已经开始面临很多关于它的警告:

SecurityPermissionAttribute is deprecated: Code Access Security is not supported or honored by the runtime

迁移它的正确方法是什么?只是删除注释或抑制警告消息?

标签: c#.net-core

解决方案


代码访问安全 (CAS) API在 .NET Core 到 .NET 5 的所有版本中都不起作用
。 换句话说:它们……什么都没有

这些类型是从 .NET Framework 4 继承的,以便更轻松地迁移代码,仅此而已 - 只是为了避免编译错误。在某些情况下,您甚至可能会得到一个PlatformNotSupportedException.

您现在可以将其全部保留,但由于它什么都不做,它只有一个目的:提醒您 (a) 您应该考虑将其删除,同时 (b) 评估放入它的原因 + 替换什么您可能想要添加的保护措施,以获得类似于它在 .NET Framework 中为您提供的保护措施。

更多信息:
https ://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/code-access-security-apis-obsolete


推荐阅读