Condividi tramite


I parametri URI non devono essere stringhe

Aggiornamento: novembre 2007

TypeName

UriParametersShouldNotBeStrings

CheckId

CA1054

Category

Microsoft.Design

Breaking Change

Breaking

Causa

Un tipo dichiara un metodo con un parametro di stringa il cui nome contiene "uri", "Uri", "urn", "Urn", "url" o "Url" e tale tipo non dichiara un overload corrispondente che accetta un parametro System.Uri.

Descrizione della regola

La regola suddivide il nome del parametro in token in base alla convenzione di denominazione Camel e controlla che ogni token sia uguale a "uri", "Uri", "urn", "Urn", "url" o "Url". Se è presente una corrispondenza, la regola presuppone che il parametro rappresenti un URI (Uniform Resource Identifier). Una rappresentazione di stringa di un URI è soggetta a errori di analisi e codifica e può creare punti di vulnerabilità della protezione. Se un metodo accetta una rappresentazione di stringa di un URI, deve essere fornito un overload corrispondente che accetti un'istanza della classe Uri, la quale fornisce questi servizi in modo sicuro e protetto.

Correzione di violazioni

Per correggere una violazione di questa regola, modificare il parametro in un tipo Uri. Si tratta di una modifica sostanziale. In alternativa, fornire un overload del metodo che accetta un parametro Uri. Si tratta di una modifica non sostanziale.

Esclusione di avvisi

L'esclusione di un avviso da questa regola è sicura se il parametro non rappresenta un URI.

Esempio

Nell'esempio riportato di seguito vengono illustrati un tipo ErrorProne che viola la regola e un tipo SaferWay che la soddisfa.

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");
      }
   };
}

Regole correlate

Le proprietà URI non devono essere stringhe

I valori restituiti URI non devono essere stringhe

Passare oggetti System.Uri invece di stringhe

Gli overload URI dei valori di stringa chiamano gli overload System.Uri