[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method)] |
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 SecurityAction passed to the constructor determines the allowable WebPermissionAttribute targets. The system uses the WebPermission returned by the WebPermissionAttribute.CreatePermission method to convert the security information of the attribute target to a serializable form stored in metadata.
// Checks the permission using declarative security for the URL. [WebPermission(SecurityAction.Demand, ConnectPattern = "http://www.contoso.com.*")] public void Connect() { // Creates a Connection. HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.contoso.com"); Console.WriteLine("Connecting to 'http://www.contoso.com'..........."); HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse(); Console.WriteLine("Connection Created Successfully:\n"); Console.WriteLine("Getting data from 'www.contoso.com.............."); StreamReader readStream = new StreamReader(myWebResponse.GetResponseStream()); Console.WriteLine("The contents of the file are :\n"); Console.WriteLine(readStream.ReadToEnd()); readStream.Close(); // Releases the response object resources. myWebResponse.Close(); }
ctor #1 | Initializes a new instance of the WebPermissionAttribute class with a value that specifies the security actions that can be performed on this class. |
Accept | Read-write Gets or sets the URI string accepted by the current WebPermissionAttribute. |
AcceptPattern | Read-write Gets or sets a regular expression pattern that describes the URI accepted by the current WebPermissionAttribute. |
Action (inherited from System.Security.Permissions.SecurityAttribute) |
Read-write See base class member description: System.Security.Permissions.SecurityAttribute.Action Gets or sets a security action. |
Connect | Read-write Gets or sets the URI connection string controlled by the current WebPermissionAttribute. |
ConnectPattern | Read-write Gets or sets a regular expression pattern that describes the URI connection controlled by the current WebPermissionAttribute. |
TypeId (inherited from System.Attribute) |
Read-only See base class member description: System.Attribute.TypeId When implemented in a derived class, gets a unique identifier for this Attribute. |
Unrestricted (inherited from System.Security.Permissions.SecurityAttribute) |
Read-write See base class member description: System.Security.Permissions.SecurityAttribute.Unrestricted Gets or sets a value indicating whether full (unrestricted) permission to the resource protected by the attribute is declared. |
CreatePermission | Overridden: Creates and returns a new instance of the WebPermission class. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Attribute) |
See base class member description: System.Attribute.GetHashCode Returns the hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
IsDefaultAttribute (inherited from System.Attribute) |
See base class member description: System.Attribute.IsDefaultAttribute When overridden in a derived class, returns an indication whether the value of this instance is the default value for the derived class. |
Match (inherited from System.Attribute) |
See base class member description: System.Attribute.Match When overridden in a derived class, returns a value indicating whether this instance equals a specified object. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public WebPermissionAttribute( |
action
// Demonstrates the declarative security for the URI. [WebPermission(SecurityAction.Deny, Connect = "http://www.contoso.com/")] public void Connect() { // Throws an exception. try{ HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create("http://www.contoso.com"); } catch(Exception e){ Console.WriteLine("Exception : " + e.ToString()); }
public string Accept {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | WebPermissionAttribute.Accept is not null when you attempt to set the value. If you wish to specify more than one Accept URI, use an additional attribute declaration statement. |
[WebPermission(SecurityAction.Deny, Accept="http://www.contoso.com/Private.htm")] [WebPermission(SecurityAction.Assert, Accept="http://www.contoso.com/Public.htm")] public static void CheckAcceptPermission(string uriToCheck) { WebPermission permissionToCheck = new WebPermission(); permissionToCheck.AddPermission(NetworkAccess.Accept, uriToCheck); permissionToCheck.Demand(); } public static void demoDenySite() { //Passes a security check. CheckAcceptPermission("http://www.contoso.com/Public.htm"); Console.WriteLine("Public page has passed Accept permission check"); try { //Throws a SecurityException. CheckAcceptPermission("http://www.contoso.com/Private.htm"); Console.WriteLine("This line will not be printed"); } catch (SecurityException e) { Console.WriteLine("Expected exception" + e.Message); } }
public string AcceptPattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | WebPermissionAttribute.AcceptPattern is not null when you attempt to set the value. If you wish to specify more than one Accept URI, use an additional attribute declaration statement. |
[WebPermission(SecurityAction.Deny, AcceptPattern="http://www.contoso\\.com/Private/.*")] [WebPermission(SecurityAction.Assert, AcceptPattern="http://www.contoso\\.com/Public/.*")] public static void CheckAcceptPermission(string uriToCheck) { WebPermission permissionToCheck = new WebPermission(); permissionToCheck.AddPermission(NetworkAccess.Accept, uriToCheck); permissionToCheck.Demand(); } public static void demoDenySite() { //Passes a security check. CheckAcceptPermission("http://www.contoso.com/Public/page.htm"); Console.WriteLine("Public page has passed Accept permission check"); try { //Throws a SecurityException. CheckAcceptPermission("http://www.contoso.com/Private/page.htm"); Console.WriteLine("This line will not be printed"); } catch (SecurityException e) { Console.WriteLine("Expected exception" + e.Message); } }
public SecurityAction Action {get; set;}
|
public string Connect {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | WebPermissionAttribute.Connect is not null when you attempt to set the value. If you wish to specify more than one Connect URI, use an additional attribute declaration statement. |
[WebPermission(SecurityAction.Deny, Connect="http://www.contoso.com/Private.htm")] [WebPermission(SecurityAction.Assert, Connect="http://www.contoso.com/Public.htm")] public static void CheckConnectPermission(string uriToCheck) { WebPermission permissionToCheck = new WebPermission(); permissionToCheck.AddPermission(NetworkAccess.Connect, uriToCheck); permissionToCheck.Demand(); } public static void demoDenySite() { //Passes a security check. CheckConnectPermission("http://www.contoso.com/Public.htm"); Console.WriteLine("Public page has passed connect permission check"); try { //Throws a SecurityException. CheckConnectPermission("http://www.contoso.com/Private.htm"); Console.WriteLine("This line will not be printed"); } catch (SecurityException e) { Console.WriteLine("Expected exception" + e.Message); } }
public string ConnectPattern {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentException | WebPermissionAttribute.ConnectPattern is not null when you attempt to set the value. If you wish to specify more than one connect URI, use an additional attribute declaration statement. |
[WebPermission(SecurityAction.Deny, ConnectPattern="http://www.contoso\\.com/Private/.*")] [WebPermission(SecurityAction.Assert, ConnectPattern="http://www.contoso\\.com/Public/.*")] public static void CheckConnectPermission(string uriToCheck) { WebPermission permissionToCheck = new WebPermission(); permissionToCheck.AddPermission(NetworkAccess.Connect, uriToCheck); permissionToCheck.Demand(); } public static void demoDenySite() { //Passes a security check. CheckConnectPermission("http://www.contoso.com/Public/page.htm"); Console.WriteLine("Public page has passed Connect permission check"); try { //Throws a SecurityException. CheckConnectPermission("http://www.contoso.com/Private/page.htm"); Console.WriteLine("This line will not be printed"); } catch (SecurityException e) { Console.WriteLine("Expected exception" + e.Message); } }
public virtual object TypeId {get;}
|
public bool Unrestricted {get; set;}
|
public override IPermission CreatePermission(); |
The security information described by WebPermissionAttribute is stored in the metadata of the attribute target, which is the class to which WebPermissionAttribute is applied. The system accesses the information at run time. The system uses the WebPermission returned by SocketPermissionAttribute.CreatePermission to convert the security information of the attribute target to a serializable form stored in metadata.
~WebPermissionAttribute(); |
public override int GetHashCode(); |
public Type GetType(); |
public virtual bool IsDefaultAttribute(); |
The implementation of this method in a derived class compares the value of this instance to a standard, default value obtained by some means, then returns a Boolean value that indicates whether the value of this instance is equal to the standard. The standard value is typically coded as a constant in the implementation, or stored programmatically in a field used by the implementation.
obj
protected object MemberwiseClone(); |
public virtual string ToString(); |