예제 #1
0
        public void CopyWithChildren()
        {
            UnionCodeGroup cgChild = new UnionCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.Unrestricted)));
            UnionCodeGroup cg      = new UnionCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));

            cg.AddChild(cgChild);
            UnionCodeGroup cg2 = (UnionCodeGroup)cg.Copy();

            Assert.AreEqual(cg.Children.Count, cg2.Children.Count, "Children");
            Assert.AreEqual(cg.ToXml().ToString(), cg2.ToXml().ToString(), "ToXml");
        }
예제 #2
0
        public void Copy()
        {
            UnionCodeGroup cg  = new UnionCodeGroup(new AllMembershipCondition(), new PolicyStatement(new PermissionSet(PermissionState.None)));
            UnionCodeGroup cg2 = (UnionCodeGroup)cg.Copy();

            Assert.AreEqual(cg.AttributeString, cg2.AttributeString, "AttributeString");
            Assert.AreEqual(cg.Children.Count, cg2.Children.Count, "Children");
            Assert.AreEqual(cg.Description, cg2.Description, "Description");
            Assert.AreEqual(cg.MergeLogic, cg2.MergeLogic, "MergeLogic");
            Assert.AreEqual(cg.Name, cg2.Name, "Name");
            Assert.AreEqual(cg.PermissionSetName, cg2.PermissionSetName, "PermissionSetName");
            Assert.AreEqual(cg.ToXml().ToString(), cg2.ToXml().ToString(), "ToXml");
        }
예제 #3
0
        public void ToFromXmlRoundtrip()
        {
            const string    ps_Name = "TestName";
            PolicyStatement ps      = new PolicyStatement(new NamedPermissionSet(ps_Name));
            UnionCodeGroup  cg      = new UnionCodeGroup(new AllMembershipCondition(), ps);

            cg.Name        = "SomeName";
            cg.Description = "Some Description";
            Assert.IsTrue(cg.Equals(cg), "Equals (itself)");
            SecurityElement se = cg.ToXml();

            UnionCodeGroup cg2 = new UnionCodeGroup(new AllMembershipCondition(), ps);

            cg2.Name        = "SomeOtherName";
            cg2.Description = "Some Other Description";
            Assert.IsTrue(!cg.Equals(cg2), "Equals (another)");

            cg2.FromXml(se);
            Assert.IsTrue(cg.Equals(cg2), "Equals (FromXml)");
        }
예제 #4
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;
        }
    }