Create PrincipalPermission for Administrators : Permissions « Security « C# / CSharp Tutorial






using System;
using System.Threading;
using System.Security;
using System.Security.Permissions;
using System.Collections.Generic;
using System.Text;
using System.Collections;

    class Program
    {
        static void Main(string[] args)
        {          
            System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
            Thread.CurrentPrincipal = new System.Security.Principal.WindowsPrincipal(wi);
            Console.WriteLine(wi.Name);
            Console.WriteLine(Thread.CurrentPrincipal.Identity.Name);
            PrincipalPermission pp = new PrincipalPermission(null, "Administrators", true);
            pp.Demand();

            PrincipalPermission pp2 = new PrincipalPermission(null, "Users", true);
            pp.Union(pp2).Demand();

            try
            {
                PrincipalPermission pp3 = new PrincipalPermission(null, "Club");
                pp3.Demand();
            }
            catch (SecurityException e)
            {
                Console.WriteLine("You do not have access to the secret club.");
            }
        }
    }








35.4.Permissions
35.4.1.UnmanagedCode element of SecurityPermission( controls the code’s ability to execute unmanaged code)
35.4.2.File IO Permission: SecurityAction.RequestRefuse, Write
35.4.3.Internet SecurityAction
35.4.4.Optional permission request for IsolatedStorageFilePermission
35.4.5.Refuse request for ReflectionPermission
35.4.6.Test if the current assembly has the specified permission
35.4.7.Specify the certification file
35.4.8.A declarative role-based security demand for the current principal
35.4.9.A declarative role-based security demand for the current principal to be a member of the roles Managers OR Developers
35.4.10.An imperative role-based security demand for the current principal to represent an identity with the name Anya AND be a member of the Managers role
35.4.11.An imperative role-based security demand for the current principal to represent an identity with the name Anya, the roles of the principal are irrelevant
35.4.12.PrincipalPolicy.Intersect/Union
35.4.13.Create PrincipalPermission for Administrators