Csharp/C Sharp by API/System.Security.Permissions/FileIOPermissionAccess

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

FileIOPermissionAccess.AllAccess

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Permissions;
class Program {
    static void Main(string[] args) {
        CodeAccessPermission permissionA = new FileIOPermission(FileIOPermissionAccess.AllAccess, @"C:\");
        CodeAccessPermission permissionB = new FileIOPermission(FileIOPermissionAccess.Read, @"C:\temp");
        if (permissionB.IsSubsetOf(permissionA)) {
            Console.WriteLine("PermissionB is a subset of PermissionA");
        } else {
            Console.WriteLine("PermissionB is NOT a subset of PermissionA");
        }
    }
}


FileIOPermissionAccess.Append

  
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Permissions;
using System.IO;
class Program {
    static void Main(string[] args) {
        CodeAccessPermission permission = new FileIOPermission(FileIOPermissionAccess.Append,@"C:\audit.txt");
        permission.Deny();
        AuditClass.Save("some data to audit");
        CodeAccessPermission.RevertDeny();
    }
}
class AuditClass {
    public static void Save(string value) {
        try {
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Append,@"C:\audit.txt");
            permission.Assert();
            FileStream stream = new FileStream(@"C:\audit.txt",FileMode.Append, FileAccess.Write);
            CodeAccessPermission.RevertAssert();
            Console.WriteLine("Data written to audit file");
        } catch {
            Console.WriteLine("Failed to write data to audit file");
        }
    }
}


FileIOPermissionAccess.Read

  

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Security;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Security.Permissions;
using System.Security.Principal;
using System.Text;
public class MainClass
{
    public static void Main()
    {
        AppDomain sandboxAd;
        Evidence ev = new Evidence();
        ev.AddAssembly(Assembly.GetExecutingAssembly());
        PermissionSet permSet = new PermissionSet(PermissionState.None);
        permSet.AddPermission(new SecurityPermission(PermissionState.Unrestricted));
        permSet.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
        sandboxAd = AppDomain.CreateDomain("Sandbox", ev, new AppDomainSetup(), permSet);
        sandboxAd.DoCallBack(SecureOperationImperative);
    }
    private static void SecureOperationImperative()
    {
        FileIOPermission p = new FileIOPermission(FileIOPermissionAccess.Read, @"C:\");
        p.Demand();
    }
}


FileIOPermissionAccess.Write

   

using System;
using System.Net;
using System.Security.Permissions;

[assembly: ReflectionPermission(SecurityAction.RequestRefuse, Unrestricted = true)]
class MainClass
{
    public static void Main()
    {
        FileIOPermission fileIOPerm = new FileIOPermission(FileIOPermissionAccess.Write, @"C:\Data");
        fileIOPerm.Demand();
    }
}