3 out of 3 rated this helpful - Rate this topic

WebPermission Class

Controls rights to access HTTP Internet resources.

System.Object
  System.Security.CodeAccessPermission
    System.Net.WebPermission

Namespace:  System.Net
Assembly:  System (in System.dll)
[SerializableAttribute]
public sealed class WebPermission : CodeAccessPermission, 
	IUnrestrictedPermission

The WebPermission type exposes the following members.

  Name Description
Public method WebPermission() Creates a new instance of the WebPermission class.
Public method WebPermission(PermissionState) Creates a new instance of the WebPermission class that passes all demands or fails all demands.
Public method WebPermission(NetworkAccess, String) Initializes a new instance of the WebPermission class with the specified access rights for the specified URI.
Public method WebPermission(NetworkAccess, Regex) Initializes a new instance of the WebPermission class with the specified access rights for the specified URI regular expression.
Top
  Name Description
Public property AcceptList This property returns an enumeration of a single accept permissions held by this WebPermission. The possible objects types contained in the returned enumeration are String and System.Text.RegularExpressions.Regex.
Public property ConnectList This property returns an enumeration of a single connect permissions held by this WebPermission. The possible objects types contained in the returned enumeration are String and System.Text.RegularExpressions.Regex.
Top
  Name Description
Public method AddPermission(NetworkAccess, String) Adds the specified URI string with the specified access rights to the current WebPermission.
Public method AddPermission(NetworkAccess, Regex) Adds the specified URI with the specified access rights to the current WebPermission.
Public method Assert Declares that the calling code can access the resource protected by a permission demand through the code that calls this method, even if callers higher in the stack have not been granted permission to access the resource. Using Assert can create security issues. (Inherited from CodeAccessPermission.)
Public method Copy Creates a copy of a WebPermission. (Overrides CodeAccessPermission.Copy().)
Public method Demand Forces a SecurityException at run time if all callers higher in the call stack have not been granted the permission specified by the current instance. (Inherited from CodeAccessPermission.)
Public method Deny Obsolete. Prevents callers higher in the call stack from using the code that calls this method to access the resource specified by the current instance. (Inherited from CodeAccessPermission.)
Public method Equals Determines whether the specified CodeAccessPermission object is equal to the current CodeAccessPermission. (Inherited from CodeAccessPermission.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method FromXml Reconstructs a WebPermission from an XML encoding. (Overrides CodeAccessPermission.FromXml(SecurityElement).)
Public method GetHashCode Gets a hash code for the CodeAccessPermission object that is suitable for use in hashing algorithms and data structures such as a hash table. (Inherited from CodeAccessPermission.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method Intersect Returns the logical intersection of two WebPermission instances. (Overrides CodeAccessPermission.Intersect(IPermission).)
Public method IsSubsetOf Determines whether the current WebPermission is a subset of the specified object. (Overrides CodeAccessPermission.IsSubsetOf(IPermission).)
Public method IsUnrestricted Checks the overall permission state of the WebPermission.
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method PermitOnly Prevents callers higher in the call stack from using the code that calls this method to access all resources except for the resource specified by the current instance. (Inherited from CodeAccessPermission.)
Public method ToString Creates and returns a string representation of the current permission object. (Inherited from CodeAccessPermission.)
Public method ToXml Creates an XML encoding of a WebPermission and its current state. (Overrides CodeAccessPermission.ToXml().)
Public method Union Returns the logical union between two instances of the WebPermission class. (Overrides CodeAccessPermission.Union(IPermission).)
Top

WebPermission provides a set of methods and properties to control access to Internet resources. You can use a WebPermission to provide either restricted or unrestricted access to your resource, based on the PermissionState that is set when the WebPermission is created.

Create a WebPermission instance by calling its constructor using one of the following sets of parameters:

The ConnectList and AcceptList hold the URIs to which you have granted access permission. To add a URI to either of these lists, use AddPermission. If you pass Accept as the NetworkAccess parameter, the URI will be added to the AcceptList. WebPermission will allow connections to your target class with URIs matching the AcceptList.

Caution note Caution

To Deny access to an Internet resource, you must Deny access to all the possible paths to that resource. A better approach is to allow access to the specific resource only. For more information about this subject, refer to the Using the Deny Method topic.

Note Note

You need to Deny access using only the resource canonical path. There is no need to use all the path's syntactical variations.

Note Note

User name and default port information is stripped from the Uri before the comparison with the regular expression argument that is supplied to the WebPermission(NetworkAccess, Regex) constructor. If the regular expression contains user information or the default port number, then all incoming Uris will fail to match the regular expression.

The following example demonstrates how to create a new instance of WebPermission using a Regex. Additional hosts are added to the connect and accept list of WebPermission. Finally, the connect and accept list are displayed to the console.



    //  Create a Regex that accepts all URLs containing the host fragment www.contoso.com.
    Regex myRegex = new Regex(@"http://www\.contoso\.com/.*");

    // Create a WebPermission that gives permissions to all the hosts containing the same host fragment.
    WebPermission myWebPermission = new WebPermission(NetworkAccess.Connect,myRegex);

    //Add connect privileges for a www.adventure-works.com.
    myWebPermission.AddPermission(NetworkAccess.Connect,"http://www.adventure-works.com");

    //Add accept privileges for www.alpineskihouse.com.
    myWebPermission.AddPermission(NetworkAccess.Accept, "http://www.alpineskihouse.com/");

    // Check whether all callers higher in the call stack have been granted the permission.
    myWebPermission.Demand();

    // Get all the URIs with Connect permission.
    IEnumerator myConnectEnum = myWebPermission.ConnectList;
    Console.WriteLine("\nThe 'URIs' with 'Connect' permission are :\n");
    while (myConnectEnum.MoveNext())
    {Console.WriteLine("\t" + myConnectEnum.Current);}

    // Get all the URIs with Accept permission.	  
    IEnumerator myAcceptEnum = myWebPermission.AcceptList;
    Console.WriteLine("\n\nThe 'URIs' with 'Accept' permission is :\n");

    while (myAcceptEnum.MoveNext())
      {Console.WriteLine("\t" + myAcceptEnum.Current);}



.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ