This article has been
excerpted from book "The Complete Visual C# Programmer's Guide" from the Authors
of C# Corner.
System.Security
The System.Security namespace provides the underlying structure of the .NET
Framework security system. It includes interfaces, attributes, exceptions, and
base classes for permissions, as well as the CodeAccessPermission class, which
defines the underlying structure of all code access permissions.
IPermission Interface
Many Permission classes implement the IPermission interface, which takes the
form shown in Listing 22.12.
Listing 22.12: IPermission Interface
interface
IPermission
{
bool IsSubsetOf(IPermission
target);
void Demand();
void DemandImmediate();
IPermission Copy();
IPermission Intersect(IPermission
target);
IPermission Union(IPermission
target);
}
ISecurityEncodable Interface
The ISecurityEncodable interface is implemented to serialize permissions to and
from XML format. Listing 22.13 shows the typical structure of the interface.
Listing 22.13: ISecurityEncodable Interface
interface
ISecurityEncodable
{
void FromXml(SecurityElement e);
SecurityElement ToXml();
}
Listing 22.14 provides an example of XML security serialization.
Listing 22.14: XML Security Serialization
// code to show XML serialization
PermissionSet ps = new
PermissionSet(PermissionState.None);
SecurityElement myDOM = ps.ToXml();
Console.WriteLine(myDom.ToString());
Permission Classes
All of the classes listed in Table 22.4 are derived from the
CodeAccessPermission class.

Table 22.4: Permission Classes in the System.Security Namespace
Table 22.5 lists the most common methods used with the permission classes. Three
of these methods-Assert, Deny, and PermitOnly-are called overrides because they
override the default behavior of the security system. When different overrides
are present in the same stack frame, the runtime processes these overrides in
the following order: PermitOnly, Deny, and Assert

Table 22.5: Commonly Used Methods of Permissions Classes
If, during the stack walk, the runtime discovers more than one override of the
same type (e.g., two calls to Assert in one stack frame), the second override
causes an exception. To replace an override, first call the appropriate revert
method (e.g., RevertAssert) and then apply the new override. Also, be aware that
each stack frame can have, at most, one permission set used for denial. The most
recent Deny function replaces all other denials for other permission sets in the
current stack frame.
In Listing 22.15, a demand for a read operation to c:\dir1\ is called. If the
system refuses that demand, an exception is thrown.
Listing 22.15: Executing the Demand method of a Permissions object
// Demand
try
{
FileIOPermission p = new
FileIOPermission(FileIOPermissionAccess.Read,"C:\\dir1\\");
p.Demand();
}
catch (SecurityException ex)
{
// catch SecurityException here
}
The code in Listing 22.16 compares two file permissions to determine whether one
is a subset of the other. If so, the subset may derive granted permissions from
the "parent." For example, if file I/O permission to c:\dir1\ is granted, then
file I/O permission to c:\dir1\dir2\ is also granted. Thus, you can conclude
that perm2 is a subset of perm1 in this example.
Listing 22.16: IsSubsetOf Example
// IsSubsetOf tests
using System;
using
System.Security;
using
System.Security.Permissions;
class
TestClass
{
public static
void Main()
{
try
{


Join the conversation! Your thoughts help the community grow.