Bagikan melalui


WebPermissionAttribute.AcceptPattern Properti

Definisi

Mendapatkan atau mengatur pola ekspresi reguler yang menjelaskan URI yang diterima oleh saat ini WebPermissionAttribute.

public:
 property System::String ^ AcceptPattern { System::String ^ get(); void set(System::String ^ value); };
public string AcceptPattern { get; set; }
member this.AcceptPattern : string with get, set
Public Property AcceptPattern As String

Nilai Properti

String yang berisi pola ekspresi reguler yang menjelaskan URI yang diterima oleh saat ini WebPermissionAttribute. String ini harus diloloskan sesuai dengan aturan untuk mengodekan Regex string konstruktor.

Pengecualian

AcceptPattern bukan null ketika Anda mencoba untuk mengatur nilai. Jika Anda ingin menentukan lebih dari satu Terima URI, gunakan pernyataan deklarasi atribut tambahan.

Contoh

Contoh berikut menunjukkan cara menggunakan WebPermissionAttribute untuk menentukan yang diizinkan AcceptPattern.

public:
   [method:WebPermission(SecurityAction::Deny,AcceptPattern="http://www\\.contoso\\.com/Private/.*")]
   static void CheckAcceptPermission( String^ uriToCheck )
   {
      WebPermission^ permissionToCheck = gcnew WebPermission;
      permissionToCheck->AddPermission( NetworkAccess::Accept, uriToCheck );
      permissionToCheck->Demand();
   }

   static void demoDenySite()
   {
      // Passes a security check.
      CheckAcceptPermission( "http://www.contoso.com/Public/page.htm" );
      Console::WriteLine( "Public page has passed Accept permission check" );

      try
      {
         // Throws a SecurityException.
         CheckAcceptPermission( "http://www.contoso.com/Private/page.htm" );
         Console::WriteLine( "This line will not be printed" );
      }
      catch ( SecurityException^ e ) 
      {
         Console::WriteLine( "Expected exception: {0}", e->Message );
      }
   }
[WebPermission(SecurityAction.Deny, AcceptPattern=@"http://www\.contoso\.com/Private/.*")]

public static void CheckAcceptPermission(string uriToCheck) {

    WebPermission permissionToCheck = new WebPermission();
    permissionToCheck.AddPermission(NetworkAccess.Accept, uriToCheck);
    permissionToCheck.Demand();
}

public static void demoDenySite() {
    //Passes a security check.
    CheckAcceptPermission("http://www.contoso.com/Public/page.htm");
    Console.WriteLine("Public page has passed Accept permission check");

    try {
        //Throws a SecurityException.
        CheckAcceptPermission("http://www.contoso.com/Private/page.htm");
        Console.WriteLine("This line will not be printed");
}
    catch (SecurityException e) {
        Console.WriteLine("Expected exception: " + e.Message);
    }
 }
<WebPermission(SecurityAction.Deny, AcceptPattern := "http://www\.contoso\.com/Private/.*")> _
  Public Shared Sub       CheckAcceptPermission(uriToCheck As String)
     Dim re As New Regex("http://www\.contoso\.com/Public/.*")
     Dim con As New WebPermission(NetworkAccess.Connect, re)
     con.Assert()
     Dim permissionToCheck As New WebPermission()
     permissionToCheck.AddPermission(NetworkAccess.Accept, uriToCheck)
     permissionToCheck.Demand()
  End Sub
  
  
  Public Shared Sub demoDenySite()
     'Passes a security check.
     CheckAcceptPermission("http://www.contoso.com/Public/page.htm")
     Console.WriteLine("Public page has passed Accept permission check")
     
     Try
        'Throws a SecurityException.
        CheckAcceptPermission("http://www.contoso.com/Private/page.htm")
        Console.WriteLine("This line will not be printed")
     Catch e As SecurityException
        Console.WriteLine(("Expected exception" + e.Message))
     End Try
  End Sub

Keterangan

Saat menerapkan WebPermissionAttribute ke kelas Anda, properti ini menentukan string ekspresi reguler apa yang akan diterima untuk digunakan dalam kelas Anda. Properti ini bersifat write-once.

Berlaku untuk

Lihat juga