Sdílet prostřednictvím


CA1056: URI vlastnosti by neměl být řetězce

Název_typu

UriPropertiesShouldNotBeStrings

CheckId

CA1056

Kategorie

Microsoft.design

Změnit rozdělení

Rozdělení

Příčina

Typ deklaruje vlastnost řetězce, jehož název obsahuje "uri", "Identifikátor Uri", "urn", "Urn", "url" nebo "Adresa Url".

Popis pravidla

Toto pravidlo rozdělí tokeny na základě úmluvy pouzdře Pascal název vlastnosti a zkontroluje, zda každý token rovná se "uri", "Identifikátor Uri", "urn", "Urn", "url" nebo "Adresa Url".Pokud odpovídající pravidlo předpokládá, že vlastnost představuje uniform resource identifier (URI).Řetězcové vyjádření identifikátoru URI je náchylné k analýze a kódování chyby a může vést k ohrožení zabezpečení.System.Uri Třída poskytuje tyto služby v bezpečném způsobem.

Jak opravit porušení

Opravit porušení tohoto pravidla, změnit vlastnost Uri typu.

Při potlačení upozornění

Je bezpečné potlačí upozornění od tohoto pravidla, pokud vlastnost nepředstavuje identifikátoru URI.

Příklad

Následující příklad ukazuje typ, ErrorProne, který porušuje pravidlo a typ, SaferWay, který splňuje pravidla.

Imports System

Namespace DesignLibrary

   Public Class ErrorProne

      Dim someUriValue As String 

      ' Violates rule UriPropertiesShouldNotBeStrings.
      Property SomeUri As String
         Get 
            Return someUriValue 
         End Get
         Set 
            someUriValue = Value 
         End Set
      End Property

      ' Violates rule UriParametersShouldNotBeStrings.
      Sub AddToHistory(uriString As String)
      End Sub

      ' Violates rule UriReturnValuesShouldNotBeStrings.
      Function GetRefererUri(httpHeader As String) As String
         Return "https://www.adventure-works.com"
      End Function

   End Class

   Public Class SaferWay

      Dim someUriValue As Uri 

      ' To retrieve a string, call SomeUri.ToString().
      ' To set using a string, call SomeUri = New Uri(string).
      Property SomeUri As Uri
         Get 
            Return someUriValue 
         End Get
         Set 
            someUriValue = Value 
         End Set
      End Property

      Sub AddToHistory(uriString As String)
         ' Check for UriFormatException.
         AddToHistory(New Uri(uriString))
      End Sub

      Sub AddToHistory(uriString As Uri)
      End Sub

      Function GetRefererUri(httpHeader As String) As Uri
         Return New Uri("https://www.adventure-works.com")
      End Function

   End Class

End Namespace
using System;

namespace DesignLibrary
{
   public class ErrorProne
   {
      string someUri;

      // Violates rule UriPropertiesShouldNotBeStrings.
      public string SomeUri
      {
         get { return someUri; }
         set { someUri = value; }
      }

      // Violates rule UriParametersShouldNotBeStrings.
      public void AddToHistory(string uriString) { }

      // Violates rule UriReturnValuesShouldNotBeStrings.
      public string GetRefererUri(string httpHeader)
      {
         return "https://www.adventure-works.com";
      }
   }

   public class SaferWay
   {
      Uri someUri;

      // To retrieve a string, call SomeUri.ToString().
      // To set using a string, call SomeUri = new Uri(string).
      public Uri SomeUri
      {
         get { return someUri; }
         set { someUri = value; }
      }

      public void AddToHistory(string uriString)
      {
         // Check for UriFormatException.
         AddToHistory(new Uri(uriString));
      }

      public void AddToHistory(Uri uriType) { }

      public Uri GetRefererUri(string httpHeader)
      {
         return new Uri("https://www.adventure-works.com");
      }
   }
}
#using <system.dll>
using namespace System;

namespace DesignLibrary
{
   public ref class ErrorProne
   {
   public:
      // Violates rule UriPropertiesShouldNotBeStrings.
      property String^ SomeUri;

      // Violates rule UriParametersShouldNotBeStrings.
      void AddToHistory(String^ uriString) { }

      // Violates rule UriReturnValuesShouldNotBeStrings.
      String^ GetRefererUri(String^ httpHeader)
      {
         return "https://www.adventure-works.com";
      }
   };

   public ref class SaferWay
   {
   public:
      // To retrieve a string, call SomeUri()->ToString().
      // To set using a string, call SomeUri(gcnew Uri(string)).
      property Uri^ SomeUri;

      void AddToHistory(String^ uriString)
      {
         // Check for UriFormatException.
         AddToHistory(gcnew Uri(uriString));
      }

      void AddToHistory(Uri^ uriType) { }

      Uri^ GetRefererUri(String^ httpHeader)
      {
         return gcnew Uri("https://www.adventure-works.com");
      }
   };
}

Souvisejících pravidel

CA1054: Parametry URI by neměl být řetězce

CA1055: URI vrátit hodnoty by neměl být řetězce

CA2234: Objekty průchod System.Uri namísto řetězce

CA1057: Řetězec identifikátoru URI přetížení volat přetížení System.Uri