Udostępnij za pośrednictwem


CA2145: Przezroczyste metod powinny nie być ozdobione SuppressUnmanagedCodeSecurityAttribute

TypeName

TransparentMethodsShouldNotUseSuppressUnmanagedCodeSecurity

CheckId

CA2145

Kategoria

Microsoft.Security

Złamanie zmiany

Złamanie

Przyczyna

Metoda przezroczyste, metoda, która jest oznaczona z SecuritySafeCriticalAttribute oznaczone metodą lub typ, który zawiera metodę SuppressUnmanagedCodeSecurityAttribute atrybut.

Opis reguły

Metody ozdobione SuppressUnmanagedCodeSecurityAttribute atrybut mają niejawne LinkDemand, nałożone na jakąkolwiek metodę, która go wymaga.LinkDemand tego wymaga, aby kod wywołujący był zabezpieczeń, krytyczne.Znakowanie metodę, która korzysta z SuppressUnmanagedCodeSecurity z SecurityCriticalAttribute atrybut powoduje, że wymaganie to bardziej oczywiste dla wywoływania metody.

Jak naprawić naruszenia

Aby naprawić naruszenie tej zasady, oznaczyć metodą lub typu z SecurityCriticalAttribute atrybut.

Kiedy do pomijania ostrzeżenia

Nie pomijaj ostrzeżenie od tej reguły.

Dd997570.collapse_all(pl-pl,VS.110).gifKod

using System;
using System.Runtime.InteropServices;
using System.Security;

namespace TransparencyWarningsDemo
{

    public class SafeNativeMethods
    {
        // CA2145 violation - transparent method marked SuppressUnmanagedCodeSecurity.  This should be fixed by
        // marking this method SecurityCritical.
        [DllImport("kernel32.dll", SetLastError = true)]
        [SuppressUnmanagedCodeSecurity]
        [return: MarshalAs(UnmanagedType.Bool)]
        internal static extern bool Beep(uint dwFreq, uint dwDuration);
    }
}