共用方式為


WebPermissionAttribute.AcceptPattern 屬性

定義

取得或設定目前 WebPermissionAttribute 可接受的 URI 其規則運算式模式描述。

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

屬性值

字串,包含目前 WebPermissionAttribute 可接受的 URI 其規則運算式模式描述。 依據 Regex 建構函式字串的編碼規則,必須逸出這個字串。

例外狀況

當您嘗試設定此值時,AcceptPattern 不是 null。 如果您想指定一個以上的可接受 URI,請使用額外的屬性宣告陳述式。

範例

下列範例示範如何使用 WebPermissionAttribute 來指定允許 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

備註

WebPermissionAttribute 用至類別時,這個屬性會指定將接受哪些正則表達式字串,以在類別中使用。 這個屬性是寫入一次。

適用於

另請參閱