This documentation is archived and is not being maintained.

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.


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

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.


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.


[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(""), HttpWebRequest)
    Console.WriteLine("This line should never be printed")


    // 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("");
        Console.WriteLine("This line should never be printed");

// Deny access to a specific resource by setting the ConnectPattern property.
[method:WebPermission(SecurityAction::Deny, ConnectPattern="")]
void Connect() {
   // Create a Connection.
   HttpWebRequest* myWebRequest =
   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.


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