Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

WebPermissionAttribute Class

Specifies permission to access Internet resources. This class cannot be inherited.

For a list of all members of this type, see WebPermissionAttribute Members.

System.Object
   System.Attribute
      System.Security.Permissions.SecurityAttribute
         System.Security.Permissions.CodeAccessSecurityAttribute
            System.Net.WebPermissionAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Assembly Or AttributeTargets.Class _
   Or AttributeTargets.Struct Or AttributeTargets.Constructor Or _
   AttributeTargets.Method)>
<Serializable>
NotInheritable Public Class WebPermissionAttribute
   Inherits CodeAccessSecurityAttribute
[C#]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
   | AttributeTargets.Struct | AttributeTargets.Constructor |
   AttributeTargets.Method)]
[Serializable]
public sealed class WebPermissionAttribute :
   CodeAccessSecurityAttribute
[C++]
[AttributeUsage(AttributeTargets::Assembly |
   AttributeTargets::Class | AttributeTargets::Struct |
   AttributeTargets::Constructor | AttributeTargets::Method)]
[Serializable]
public __gc __sealed class WebPermissionAttribute : public
   CodeAccessSecurityAttribute
[JScript]
public
   AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class |
   AttributeTargets.Struct | AttributeTargets.Constructor |
   AttributeTargets.Method)
 Serializable
class WebPermissionAttribute extends
   CodeAccessSecurityAttribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

WebPermissionAttribute allows you to declaratively specify which URI strings and regular expression strings your class can use.

The security information specified in the WebPermissionAttribute is stored in the metadata of the attribute target, which is the class to which WebPermissionAttribute is applied. The system accesses this information at run time. The System.Security.Permissions.SecurityAction passed to the constructor determines the allowable WebPermissionAttribute targets. The system uses the WebPermission returned by the CreatePermission method to convert the security information of the attribute target to a serializable form stored in metadata.

Note    WebPermissionAttribute is used only for Declarative Security. For Imperative Security, use the corresponding WebPermission.

Example

[Visual Basic, C#, C++] The following example demonstrates how to apply WebPermissionAttribute to a method.

[Visual Basic] 

    ' Deny access to a specific resource by setting the ConnectPattern property. 
    <WebPermission(SecurityAction.Deny, ConnectPattern := "http://www\.contoso\.com/.*")> Public Sub Connect()
    
    ' Create a Connection.     
    Dim myWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
    Console.WriteLine("This line should never be printed")
       

[C#] 

    // Deny access to a specific resource by setting the ConnectPattern property. 
   [WebPermission(SecurityAction.Deny, ConnectPattern=@"http://www\.contoso\.com/")]

public void Connect() 
     {
        // Create a Connection.  
        HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.contoso.com");
        Console.WriteLine("This line should never be printed");
     }


[C++] 
// Deny access to a specific resource by setting the ConnectPattern property.
[method:WebPermission(SecurityAction::Deny, ConnectPattern="http://www.contoso.com/")]
void Connect() {
   // Create a Connection.
   HttpWebRequest* myWebRequest =
      dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.contoso.com"));
   Console::WriteLine(S"This line should never be printed");
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Net

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

WebPermissionAttribute Members | System.Net Namespace

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.