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。 如果希望指定多个 Accept 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 类时,此属性指定接受在类中使用的正则表达式字符串。 此属性为写入一次。

适用于

另请参阅