共用方式為


CA2142:透明程式碼不可以使用 LinkDemand 加以保護

型別名稱

TransparentMethodsShouldNotBeProtectedWithLinkDemands

CheckId

CA2142

分類

Microsoft.Security

中斷變更

中斷

原因

透明方法需要 LinkDemand 或其他安全性要求。

規則描述

需要 LinkDemand 才能存取的透明方法會引發這個規則。 安全性透明程式碼不應負責驗證作業的安全性,因此不應要求權限。 由於透明方法的安全性應該是中立的,因此不應進行任何安全性決策。 此外,安全關鍵程式碼 (不會進行安全性決策) 不應該依賴透明程式碼在先前進行此類決策。

如何修正違規

若要修正此規則的違規情形,請移除透明方法的連結要求,若方法執行安全性檢查 (例如安全性要求),則在方法標記 SecuritySafeCriticalAttribute 屬性。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

在下列範例中,由於方法是透明的而且標記了包含 LinkDemand 的 LinkDemand PermissionSet,因此會引發規則。

using System;
using System.Security.Permissions;

namespace TransparencyWarningsDemo
{

    public class TransparentMethodsProtectedWithLinkDemandsClass
    {
        // CA2142 violation - transparent code using a LinkDemand.  This can be fixed by removing the LinkDemand
        // from the method.
        [PermissionSet(SecurityAction.LinkDemand, Unrestricted = true)]
        public void TransparentMethod()
        {
        }
    }
}

請勿隱藏此規則的警告。