WebPermission Class
Controls rights to access HTTP Internet resources.
For a list of all members of this type, see WebPermission Members.
System.Object
System.Security.CodeAccessPermission
System.Net.WebPermission
[Visual Basic] <Serializable> NotInheritable Public Class WebPermission Inherits CodeAccessPermission Implements IUnrestrictedPermission [C#] [Serializable] public sealed class WebPermission : CodeAccessPermission, IUnrestrictedPermission [C++] [Serializable] public __gc __sealed class WebPermission : public CodeAccessPermission, IUnrestrictedPermission [JScript] public Serializable class WebPermission extends CodeAccessPermission implements IUnrestrictedPermission
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
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 set when the WebPermission is created.
Create a WebPermission instance by calling its constructor using one of the following sets of parameters:
- No parameters. The default PermissionState is None.
- A PermissionState. Specify either Unrestricted to allow any URI to be used in the target class, or None to allow access only to URIs you specify through the use of the AddPermission method.
- A NetworkAccess value and a URI string. The specified URI has permissions granted by the NetworkAccess value.
- A NetworkAccess specifier and URI regular expression.
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. A better approach is to allow access to the specific resource only. For more information on this subject refer to the Deny topic.
Note You only need to Deny access using the resource canonical path. There is no need to use all the path's syntactical variations.
Example
[Visual Basic, C#, C++] 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.
[Visual Basic] ' 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 [C#] // 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);} [C++] // Create a Regex that accepts all URLs containing the host fragment www.contoso.com. Regex* myRegex = new Regex(S"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,S"http://www.adventure-works.com"); //Add accept privileges for www.alpineskihouse.com. myWebPermission->AddPermission(NetworkAccess::Accept, S"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(S"\nThe 'URIs' with 'Connect' permission are :\n"); while (myConnectEnum->MoveNext()) {Console::WriteLine(S"\t{0}", myConnectEnum->Current);} // Get all the URIs with Accept permission. IEnumerator* myAcceptEnum = myWebPermission->AcceptList; Console::WriteLine(S"\n\nThe 'URIs' with 'Accept' permission is :\n"); while (myAcceptEnum->MoveNext()) {Console::WriteLine(S"\t{0}", myAcceptEnum->Current);}
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
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
WebPermission Members | System.Net Namespace | CodeAccessPermission | PermissionState | NetworkAccess