WebPermissionAttribute.ConnectPattern 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置描述当前 WebPermissionAttribute 控制的 URI 连接的正则表达式模式。
public:
property System::String ^ ConnectPattern { System::String ^ get(); void set(System::String ^ value); };
public string ConnectPattern { get; set; }
member this.ConnectPattern : string with get, set
Public Property ConnectPattern As String
属性值
一个字符串,包含描述该 WebPermissionAttribute 控制的 URI 连接的正则表达式模式。
例外
当尝试设置该值时,ConnectPattern 不为 null
。 如果希望指定多个 Connect URI,请使用附加的特性声明语句。
示例
以下示例演示如何使用 WebPermissionAttribute 指定允许的 ConnectPattern。
public:
// Set the WebPermissionAttribute ConnectPattern property.
[WebPermission(SecurityAction::Deny,ConnectPattern="http://www\\.contoso\\.com/Private/.*")]
static void CheckConnectPermission( String^ uriToCheck )
{
WebPermission^ permissionToCheck = gcnew WebPermission;
permissionToCheck->AddPermission( NetworkAccess::Connect, uriToCheck );
permissionToCheck->Demand();
}
static void demoDenySite()
{
//Pass the security check.
CheckConnectPermission( "http://www.contoso.com/Public/page.htm" );
Console::WriteLine( "Public page has passed Connect permission check" );
try
{
//Throw a SecurityException.
CheckConnectPermission( "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 );
}
}
};
// Set the WebPermissionAttribute ConnectPattern property.
[WebPermission(SecurityAction.Deny, ConnectPattern=@"http://www\.contoso\.com/Private/.*")]
public static void CheckConnectPermission(string uriToCheck)
{
WebPermission permissionToCheck = new WebPermission();
permissionToCheck.AddPermission(NetworkAccess.Connect, uriToCheck);
permissionToCheck.Demand();
}
public static void demoDenySite() {
//Pass the security check.
CheckConnectPermission("http://www.contoso.com/Public/page.htm");
Console.WriteLine("Public page has passed Connect permission check");
try
{
//Throw a SecurityException.
CheckConnectPermission("http://www.contoso.com/Private/page.htm");
Console.WriteLine("This line will not be printed");
}
catch (SecurityException e)
{
Console.WriteLine("Expected exception" + e.Message);
}
}
' Set the WebPermissionAttribute ConnectPattern property.
<WebPermission(SecurityAction.Deny, ConnectPattern := "http://www\.contoso\.com/Private/.*")> _
Public Shared Sub CheckConnectPermission(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.Connect, uriToCheck)
permissionToCheck.Demand()
End Sub
Public Shared Sub demoDenySite()
'Pass the security check.
CheckConnectPermission("http://www.contoso.com/Public/page.htm")
Console.WriteLine("Public page has passed Connect permission check")
Try
'Throw a SecurityException.
CheckConnectPermission("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 类时,此属性指定接受在类中使用的正则表达式连接字符串。 此属性为写入一次。