Beispiel #1
0
    public static void CreateAPolicyLevel()
    {
        try
        {
            //<Snippet2>
            // Create an AppDomain policy level.
            PolicyLevel pLevel = PolicyLevel.CreateAppDomainLevel();
            //</Snippet2>
            // The root code group of the policy level combines all
            // permissions of its children.
            UnionCodeGroup rootCodeGroup;
            PermissionSet  ps = new PermissionSet(PermissionState.None);
            ps.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));

            rootCodeGroup = new UnionCodeGroup(
                new AllMembershipCondition(),
                new PolicyStatement(ps, PolicyStatementAttribute.Nothing));

            // This code group grants FullTrust to assemblies with the strong
            // name key from this assembly.
            UnionCodeGroup myCodeGroup = new UnionCodeGroup(
                new StrongNameMembershipCondition(
                    new StrongNamePublicKeyBlob(GetKey()),
                    null,
                    null),
                new PolicyStatement(new PermissionSet(PermissionState.Unrestricted),
                                    PolicyStatementAttribute.Nothing)
                );
            myCodeGroup.Name = "My CodeGroup";


            //<Snippet4>
            // Add the code groups to the policy level.
            rootCodeGroup.AddChild(myCodeGroup);
            pLevel.RootCodeGroup = rootCodeGroup;
            Console.WriteLine("Permissions granted to all code running in this AppDomain level: ");
            Console.WriteLine(rootCodeGroup.ToXml());
            Console.WriteLine("Child code groups in RootCodeGroup:");
            IList       codeGroups = pLevel.RootCodeGroup.Children;
            IEnumerator codeGroup  = codeGroups.GetEnumerator();
            while (codeGroup.MoveNext())
            {
                Console.WriteLine("\t" + ((CodeGroup)codeGroup.Current).Name);
            }
            //</Snippet4>
            //<Snippet5>
            Console.WriteLine("Demonstrate adding and removing named permission sets.");
            Console.WriteLine("Original named permission sets:");
            ListPermissionSets(pLevel);
            NamedPermissionSet myInternet = pLevel.GetNamedPermissionSet("Internet");
            //</Snippet5>
            myInternet.Name = "MyInternet";
            //<Snippet6>
            pLevel.AddNamedPermissionSet(myInternet);
            //</Snippet6>
            Console.WriteLine("\nNew named permission sets:");
            ListPermissionSets(pLevel);
            myInternet.RemovePermission(typeof(System.Security.Permissions.FileDialogPermission));
            //<Snippet7>
            pLevel.ChangeNamedPermissionSet("MyInternet", myInternet);
            //</Snippet7>
            //<Snippet8>
            pLevel.RemoveNamedPermissionSet("MyInternet");
            //</Snippet8>
            Console.WriteLine("\nCurrent permission sets:");
            ListPermissionSets(pLevel);
            pLevel.AddNamedPermissionSet(myInternet);
            Console.WriteLine("\nUpdated named permission sets:");
            ListPermissionSets(pLevel);
            //<Snippet9>
            pLevel.Reset();
            //</Snippet9>
            Console.WriteLine("\nReset named permission sets:");
            ListPermissionSets(pLevel);
            //<Snippet10>
            Console.WriteLine("\nType property = " + pLevel.Type.ToString());
            //</Snippet10>
            //<Snippet11>
            Console.WriteLine("The result of GetHashCode is " + pLevel.GetHashCode().ToString());
            //</Snippet11>
            Console.WriteLine("StoreLocation property for the AppDomain level is empty, since AppDomain policy " +
                              "cannot be saved to a file.");
            Console.WriteLine("StoreLocation property = " + pLevel.StoreLocation);
            //<Snippet12>
            PolicyLevel pLevelCopy = PolicyLevel.CreateAppDomainLevel();
            // Create a copy of the PolicyLevel using ToXml/FromXml.
            pLevelCopy.FromXml(pLevel.ToXml());

            if (ComparePolicyLevels(pLevel, pLevelCopy))
            {
                Console.WriteLine("The ToXml/FromXml roundtrip was successful.");
            }
            else
            {
                Console.WriteLine("ToXml/FromXml roundtrip failed.");
            }
            //</Snippet12>
            Console.WriteLine("Show the result of resolving policy for evidence unique to the AppDomain policy level.");
            Evidence myEvidence = new Evidence(new object[] { myCodeGroup }, null);
            CheckEvidence(pLevel, myEvidence);
            return;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            return;
        }
    }