Compartir a través de


CA2123: Las peticiones de vínculos de reemplazo deberían ser idénticas a la base

Nombre de tipo

OverrideLinkDemandsShouldBeIdenticalToBase

Identificador de comprobación

CA2123

Categoría

Microsoft.Security

Cambio problemático

Motivo

Un método público o protegido de un tipo público reemplaza a un método o implementa una interfaz, y no tiene la misma Peticiones de vínculos que la interfaz y el método virtual.

Descripción de la regla

Esta regla compara un método con su método base, que es una interfaz o un método virtual de otro tipo y, a continuación, compara las solicitudes de vínculos en cada uno.Se crea un informe de infracción si el método o el método base tiene una petición de vínculo y el otro no.

Si se infringe esta regla, un llamador malintencionado puede omitir la solicitud de vínculo simplemente llamando al método no seguro.

Cómo corregir infracciones

Para corregir una infracción de esta regla, aplique la misma solicitud de vínculo al método de reemplazo o a la implementación.Si no es posible, marque el método con una demanda completa o quita el atributo.

Cuándo suprimir advertencias

No suprima las advertencias de esta regla.

Ejemplo

El ejemplo siguiente muestra distintas infracciones de esta regla.

using System.Security;
using System.Security.Permissions;
using System;

namespace SecurityRulesLibrary
{
   public interface ITestOverrides
   {  
      [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
      Object GetFormat(Type formatType);
   }

   public class OverridesAndSecurity : ITestOverrides
   {
      // Rule violation: The interface has security, and this implementation does not.
      object ITestOverrides.GetFormat(Type formatType)
      {
         return (formatType == typeof(OverridesAndSecurity) ? this : null);
      }

      // These two methods are overridden by DerivedClass and DoublyDerivedClass.
      [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
      public virtual void DoSomething()
      {
         Console.WriteLine("Doing something.");
      }

      public virtual void DoSomethingElse()
      {
         Console.WriteLine("Doing some other thing.");
      }
   }

   public class DerivedClass : OverridesAndSecurity, ITestOverrides
   {
      //  Rule violation: The interface has security, and this implementation does not.
      public object GetFormat(Type formatType)
      {
         return (formatType == typeof(OverridesAndSecurity) ? this : null);
      }

      // Rule violation: This does not have security, but the base class version does.
      public override void DoSomething()
      {
         Console.WriteLine("Doing some derived thing.");
      }

      // Rule violation: This has security, but the base class version does not.
      [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
      public override void DoSomethingElse()
      {
         Console.WriteLine("Doing some other derived thing.");
      }
   }

   public class DoublyDerivedClass : DerivedClass
   {
      // The OverridesAndSecurity version of this method does not have security. 
      // Base class DerivedClass's version does. 
      // The DoublyDerivedClass version does not violate the rule, but the 
      // DerivedClass version does violate the rule.
      public override void DoSomethingElse()
      {
         Console.WriteLine("Doing some other derived thing.");
      }
   }
}

Vea también

Conceptos

Peticiones de vínculos

Otros recursos

Instrucciones de codificación segura