{
public static
void Main()
{
// Try to access resources using the
permissions
// currently available.
AttemptAccess("Default permissions");
// Create a permission set that allows read
access to
// the TEMP
// environment variable and read,
write, and append
PermissionSet ps =
new PermissionSet(PermissionState.None);
ps.AddPermission(new
EnvironmentPermission(EnvironmentPermissionAccess.Read,
"TEMP"));
ps.AddPermission(new
FileIOPermission(FileIOPermissionAccess.Read
|
FileIOPermissionAccess.Write |
FileIOPermissionAccess.Append,
@"c:\myfile.txt"));
// test if we can access the resource
contained in ps
ps.Assert();
// Try to access resources using the
permissions we've
// just asserted.
AttemptAccess("Assert permissions.");
// Remove this stack frame's Assert
CodeAccessPermission.RevertAssert();
// Deny access to the resources we specify
ps.Deny();
// Try to access resources using the
permissions we've
// just denied.
AttemptAccess("Deny permissions.");
// Remove this stack frame's Deny so we're
back to
// default permissions.
CodeAccessPermission.RevertDeny();
// Make the permissions indicate the only
things that
// we're allowed to do.
ps.PermitOnly();
// Try to access resources using only the
permissions
// we've just permitted.
AttemptAccess("PermitOnly
permissions.");
// Remove this stack frame's PermitOnly so
we're back
// to default permissions.
CodeAccessPermission.RevertPermitOnly();
// Remove the FileIOPermissions from the
permission set
ps.RemovePermission(typeof(FileIOPermission));
// Try to access resources using only the
Environment
// permissions.
ps.PermitOnly();
AttemptAccess("PermitOnly without
FileIOPermission permissions.");
// Remove this stack frame's PermitOnly so
we're back
// to default permissions.
CodeAccessPermission.RevertPermitOnly();
// Remove the EnvironmentPermission from the
permission
// set
ps.RemovePermission(typeof(EnvironmentPermission));
// Try to access resources using no
permissions.
ps.PermitOnly();
AttemptAccess("PermitOnly without any
permissions");
// Remove this stack frame's PermitOnly so
we're back
// to default permissions.
CodeAccessPermission.RevertPermitOnly();
Console.ReadLine();
}
public static
void AttemptAccess(String
strMsg)
{
Console.WriteLine(strMsg);
FileStream fs =
null;
String env =
null;
// Try to access a file
try
{
fs = new
FileStream("SomeFile",
FileMode.OpenOrCreate);
}
catch (Exception)
{
// ignore catch
}
// Try to read an environment variable
try
{
env = Environment.GetEnvironmentVariable("TEMP");
}
catch (Exception)
{
// ignore catch
}
// Display what we sucessfully did and what we
failed.
Console.WriteLine("test
results: " + ((fs != null) ?
"File opened." :
"File cannot be opened!") +
" " +
((env != null) ?
"Environment read." :
"Environment cannot be read!"));
// close file stream
if (fs !=
null)
{
fs.Close();
}
}
}
// RevertDeny
public
interface anyinterface
{
void anyfunction(char
ch1);
}
public class
myclass:anyinterface
{
PermissionSet ps =
new PermissionSet(PermissionState.None);
ps.AddPermission(new
FileIOPermission(FileIOPermissionAccess.AllAccess,@"c:\dir1"));
ps.AddPermission(new
FileIOPermission(FileIOPermissionAccess.AllAccess,
@"c:\dir2"));
ps.AddPermission(new
EnvironmentPermission(PermissionState.Unrestricted));
// deny the permissions we just created
ps.Deny();
anyfunction('a');
// revoke denial of the permissions just
created
CodeAccessPermission.RevertDeny();
}