共用方式為


SYSLIB0003:不支援程式碼存取安全

程式碼存取安全(CAS) 是一項未受支援的舊有技術。 啟用 CAS 的基礎架構僅存在於 .NET Framework 2.x 至 4.x,已棄用且未獲得服務或安全修補。

因此,從 .NET 5 開始,大多數與程式碼存取安全(CAS)相關的類型已過時。 這包括 CAS 屬性,例如 SecurityPermissionAttribute、CAS 許可權物件,例如 SocketPermissionEvidenceBase衍生型別和其他支援的 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>

如需詳細資訊,請參閱隱藏警告

另請參閱