WebPermission WebPermission WebPermission WebPermission Class

Definition

Controls rights to access HTTP Internet resources.

public ref class WebPermission sealed : System::Security::CodeAccessPermission, System::Security::Permissions::IUnrestrictedPermission
public sealed class WebPermission : System.Security.CodeAccessPermission, System.Security.Permissions.IUnrestrictedPermission
type WebPermission = class
    inherit CodeAccessPermission
    interface IUnrestrictedPermission
Public NotInheritable Class WebPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
Inheritance
Implements

Examples

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 = gcnew Regex( "http://www\\.contoso\\.com/.*" );

// Create a WebPermission that gives permissions to all the hosts containing the same host fragment.
WebPermission^ myWebPermission = gcnew 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{0}", 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{0}", myAcceptEnum->Current );
}

//  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);}

' Create a Regex that accepts all the URLs contianing the host fragment www.contoso.com.
Dim myRegex As New Regex("http://www\.contoso\.com/.*")
  
' Create a WebPermission that gives permission to all the hosts containing same host fragment.
Dim myWebPermission As 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.
Dim myConnectEnum As IEnumerator = myWebPermission.ConnectList
Console.WriteLine(ControlChars.NewLine + "The 'URIs' with 'Connect' permission are :" + ControlChars.NewLine)
While myConnectEnum.MoveNext()
  Console.WriteLine((ControlChars.Tab + myConnectEnum.Current.ToString()))
End While 

' Get all the URIs with Accept permission.	  
Dim myAcceptEnum As IEnumerator = myWebPermission.AcceptList
Console.WriteLine(ControlChars.NewLine + ControlChars.NewLine + "The 'URIs' with 'Accept' permission is :" + ControlChars.NewLine)

While myAcceptEnum.MoveNext()
  Console.WriteLine((ControlChars.Tab + myAcceptEnum.Current))
End While

Remarks

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

To deny access to an Internet resource, you must deny access to all the possible paths to that resource. This requires calling WebPermission.WebPermission with state parameter set to Deny. A better approach is to allow access to the specific resource only. For more information about this subject, refer to the NIB: Using the Deny Method topic.

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

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.

Constructors

WebPermission() WebPermission() WebPermission() WebPermission()

Creates a new instance of the WebPermission class.

WebPermission(NetworkAccess, Regex) WebPermission(NetworkAccess, Regex) WebPermission(NetworkAccess, Regex) WebPermission(NetworkAccess, Regex)

Initializes a new instance of the WebPermission class with the specified access rights for the specified URI regular expression.

WebPermission(NetworkAccess, String) WebPermission(NetworkAccess, String) WebPermission(NetworkAccess, String) WebPermission(NetworkAccess, String)

Initializes a new instance of the WebPermission class with the specified access rights for the specified URI.

WebPermission(PermissionState) WebPermission(PermissionState) WebPermission(PermissionState) WebPermission(PermissionState)

Creates a new instance of the WebPermission class that passes all demands or fails all demands.

Properties

AcceptList AcceptList AcceptList 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 Regex.

ConnectList ConnectList ConnectList 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 Regex.

Methods

AddPermission(NetworkAccess, Regex) AddPermission(NetworkAccess, Regex) AddPermission(NetworkAccess, Regex) AddPermission(NetworkAccess, Regex)

Adds the specified URI with the specified access rights to the current WebPermission.

AddPermission(NetworkAccess, String) AddPermission(NetworkAccess, String) AddPermission(NetworkAccess, String) AddPermission(NetworkAccess, String)

Adds the specified URI string with the specified access rights to the current WebPermission.

Assert() Assert() Assert() 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)
Copy() Copy() Copy() Copy()

Creates a copy of a WebPermission.

Demand() Demand() Demand() 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)
Deny() Deny() Deny() Deny()

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)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified CodeAccessPermission object is equal to the current CodeAccessPermission.

(Inherited from CodeAccessPermission)
FromXml(SecurityElement) FromXml(SecurityElement) FromXml(SecurityElement) FromXml(SecurityElement)

Reconstructs a WebPermission from an XML encoding.

GetHashCode() GetHashCode() GetHashCode() 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)
GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Intersect(IPermission) Intersect(IPermission) Intersect(IPermission) Intersect(IPermission)

Returns the logical intersection of two WebPermission instances.

IsSubsetOf(IPermission) IsSubsetOf(IPermission) IsSubsetOf(IPermission) IsSubsetOf(IPermission)

Determines whether the current WebPermission is a subset of the specified object.

IsUnrestricted() IsUnrestricted() IsUnrestricted() IsUnrestricted()

Checks the overall permission state of the WebPermission.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
PermitOnly() PermitOnly() PermitOnly() 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)
ToString() ToString() ToString() ToString()

Creates and returns a string representation of the current permission object.

(Inherited from CodeAccessPermission)
ToXml() ToXml() ToXml() ToXml()

Creates an XML encoding of a WebPermission and its current state.

Union(IPermission) Union(IPermission) Union(IPermission) Union(IPermission)

Returns the logical union between two instances of the WebPermission class.

Explicit Interface Implementations

IPermission.Demand() IPermission.Demand() IPermission.Demand() IPermission.Demand() Inherited from CodeAccessPermission
IStackWalk.Assert() IStackWalk.Assert() IStackWalk.Assert() IStackWalk.Assert() Inherited from CodeAccessPermission
IStackWalk.Demand() IStackWalk.Demand() IStackWalk.Demand() IStackWalk.Demand() Inherited from CodeAccessPermission
IStackWalk.Deny() IStackWalk.Deny() IStackWalk.Deny() IStackWalk.Deny() Inherited from CodeAccessPermission
IStackWalk.PermitOnly() IStackWalk.PermitOnly() IStackWalk.PermitOnly() IStackWalk.PermitOnly() Inherited from CodeAccessPermission

Applies to

See Also