SYSLIB0003:不支持代码访问安全性

代码访问安全性 (CAS) 是一项不受支持的传统技术。 用于启用 CAS 的基础结构(仅存在于 .NET Framework 2.x - 4.x)已弃用,并且不接受服务或安全修补。

因此,从 .NET 5 开始,.NET 中大多数与代码访问安全性 (CAS) 相关的类型均已过时。 这包含 CAS 属性(如 SecurityPermissionAttribute)、CAS 权限对象(如 SocketPermission)、EvidenceBase 派生类型和其他支持 API。 使用这些 API 会在编译时生成警告 SYSLIB0003

已过时 CAS API 的完整列表如下所示:

工作区

  • 如果要断言任何安全权限,请删除断言该权限的属性或调用。

    // REMOVE the attribute below.
    [SecurityPermission(SecurityAction.Assert, ControlThread = true)]
    public void DoSomething()
    {
    }
    public void DoAssert()
    {
        // REMOVE the line below.
        new SecurityPermission(SecurityPermissionFlag.ControlThread).Assert();
    }
    
  • 如果要拒绝或限制(通过 PermitOnly)任何权限,请与安全顾问联系。 由于 .NET 5+ 运行时不支持 CAS 属性,因此如果应用程序错误地依赖于 CAS 基础结构来限制对这些方法的访问,则它可能存在安全漏洞。

    // REVIEW the attribute below; could indicate security vulnerability.
    [SecurityPermission(SecurityAction.Deny, ControlThread = true)]
    public void DoSomething()
    {
    }
    public void DoPermitOnly()
    {
        // REVIEW the line below; could indicate security vulnerability.
        new SecurityPermission(SecurityPermissionFlag.ControlThread).PermitOnly();
    }
    
  • 如果要求任何权限(除 PrincipalPermission 外),请删除该请求。 所有请求都将在运行时成功。

    // REMOVE the attribute below; it will always succeed.
    [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
    public void DoSomething()
    {
    }
    public void DoDemand()
    {
        // REMOVE the line below; it will always succeed.
        new SecurityPermission(SecurityPermissionFlag.ControlThread).Demand();
    }
    
  • 如果要求 PrincipalPermission,请参阅 SYSLIB0002:PrincipalPermissionAttribute 已过时指南。 本指南适用于 PrincipalPermissionPrincipalPermissionAttribute

抑制警告

如果必须使用已过时的 API,可在代码或项目文件中禁止显示警告。

若只想抑制单个冲突,请将预处理器指令添加到源文件以禁用该规则,然后重新启用警告。

// Disable the warning.
#pragma warning disable SYSLIB0003

// Code that uses obsolete API.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB0003

若要禁止显示项目中的所有 SYSLIB0003 警告,请将属性 <NoWarn> 添加到项目文件。

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   ...
   <NoWarn>$(NoWarn);SYSLIB0003</NoWarn>
  </PropertyGroup>
</Project>

有关详细信息,请参阅取消警告

另请参阅