Csharp/C Sharp/Security/PrincipalPermission

Материал из .Net Framework эксперт
Версия от 11:38, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Demand PrincipalPermission

 
using System;
using System.Security;
using System.Security.Principal;
using System.Security.Permissions;

class Class1 {
    static void Main(string[] args) {
        //AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        WindowsIdentity wi = WindowsIdentity.GetCurrent();
        PrincipalPermission PrincipalPerm = new PrincipalPermission(wi.Name, "Administrator");
        try {
            PrincipalPerm.Demand();
            Console.WriteLine("Code demand for an administrator succeeded.");
        } catch (SecurityException e) {
            Console.WriteLine("Demand for administrator failed!");
            Console.WriteLine(e.Message);
        }
    }
}


new PrincipalPermission(null, @"MACHINE\Managers")

 
using System;
using System.Security.Permissions;
class MainClass {
    public static void Method1() {
        PrincipalPermission perm = new PrincipalPermission(@"MACHINE\Tester", null);
        perm.Demand();
    }

}


Union two PrincipalPermissions

 
using System;
using System.Security.Permissions;
class MainClass {

    public static void Method2() {
        PrincipalPermission perm1 = new PrincipalPermission(null, @"MACHINE\Managers");
        PrincipalPermission perm2 = new PrincipalPermission(null, @"MACHINE\Developers");
        perm1.Union(perm2).Demand();
    }
    public static void Method3() {
        PrincipalPermission perm = new PrincipalPermission(@"MACHINE\Tester", @"MACHINE\Managers");
        perm.Demand();
    }
    [PrincipalPermission(SecurityAction.Demand, Name = @"MACHINE\Tester")]
    public static void Method4() {
    }
    [PrincipalPermission(SecurityAction.Demand, Role = @"MACHINE\Managers")]
    [PrincipalPermission(SecurityAction.Demand, Role = @"MACHINE\Developers")]
    public static void Method5() {
        // Method implementation. . .
    }
    [PrincipalPermission(SecurityAction.Demand, Name = @"MACHINE\Tester",
        Role = @"MACHINE\Managers")]
    public static void Method6() {
        // Method implementation. . .
    }
}