Exemplo n.º 1
0
        private static void CreateAPolicyLevel()
        {
            try
            {
                // Create an AppDomain policy level.
                PolicyLevel pLevel = PolicyLevel.CreateAppDomainLevel();

                // 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";

                // 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);
                }
                Console.WriteLine("Demonstrate adding and removing named permission sets.");
                Console.WriteLine("Original named permissions sets:");
                ListPermissionSets(pLevel);
                NamedPermissionSet myInternet = pLevel.GetNamedPermissionSet("Internet");
                    

            }
            catch
            {
            }
        }
Exemplo n.º 2
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");
		}
Exemplo n.º 3
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");
		}
Exemplo n.º 4
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)");
		}