Example #1
0
    public static void Main(string[] args)
    {
        //<Snippet2>
        Console.WriteLine("Creating a permission with the Flags property =" +
                          " ProtectData.");
        DataProtectionPermission sp = new DataProtectionPermission(
            DataProtectionPermissionFlags.ProtectData);

        sp.PermitOnly();
        //</Snippet2>

        // Protect the data
        ProtectData();
        // This should fail without the correct permission
        UnprotectData();
        // Revert the permission that limited access
        CodeAccessPermission.RevertPermitOnly();

        // This should now work.
        UnprotectData();
        // Demonstrate the behavior of the class members.
        ShowMembers();

        Console.WriteLine("Press the Enter key to exit.");
        Console.ReadKey();
        return;
    }
Example #2
0
        public void ExecuteSomethingFromNetFrameworkLibrary()
        {
            var uselessStuff = new DataProtectionPermission(PermissionState.None);

            var someStuff = uselessStuff.Flags;

            _logger.LogInformation("Use some stuff from .NET Library: {someStuff}", someStuff);
        }
Example #3
0
        public void Unrestricted()
        {
            DataProtectionPermissionAttribute a = Empty();

            a.Unrestricted = true;
            Assert.AreEqual(DataProtectionPermissionFlags.NoFlags, a.Flags, "Unrestricted");

            DataProtectionPermission perm = (DataProtectionPermission)a.CreatePermission();

            Assert.AreEqual(DataProtectionPermissionFlags.AllFlags, perm.Flags, "CreatePermission.Flags");
        }
Example #4
0
        public static void DataProtectionPermissionCallMethods()
        {
            DataProtectionPermission dp         = new DataProtectionPermission(new PermissionState());
            bool isunrestricted                 = dp.IsUnrestricted();
            DataProtectionPermissionFlags flags = dp.Flags;
            IPermission other = new DataProtectionPermission(flags);

            other = dp.Copy();
            other = dp.Union(other);
            other = dp.Intersect(other);
            bool            isSubsetOf = dp.IsSubsetOf(other);
            SecurityElement se         = dp.ToXml();

            dp.FromXml(se);
        }
Example #5
0
        public void Default()
        {
            DataProtectionPermissionAttribute a = new DataProtectionPermissionAttribute(SecurityAction.Assert);

            Assert.IsFalse(a.ProtectData, "ProtectData");
            Assert.IsFalse(a.UnprotectData, "UnprotectData");
            Assert.IsFalse(a.ProtectMemory, "ProtectMemory");
            Assert.IsFalse(a.UnprotectMemory, "UnprotectMemory");
            Assert.IsFalse(a.Unrestricted, "Unrestricted");

            Assert.AreEqual(DataProtectionPermissionFlags.NoFlags, a.Flags, "Flags");
            Assert.AreEqual(a.ToString(), a.TypeId.ToString(), "TypeId");

            DataProtectionPermission perm = (DataProtectionPermission)a.CreatePermission();

            Assert.AreEqual(DataProtectionPermissionFlags.NoFlags, perm.Flags, "CreatePermission.Flags");
        }
Example #6
0
    // The following method is intended to demonstrate only the behavior of
    // DataProtectionPermission class members,and not their practical usage.
    // Most properties and methods in this class are used for the resolution
    // and enforcement of security policy by the security infrastructure code.
    private static void ShowMembers()
    {
        Console.WriteLine("Creating four DataProtectionPermissions");
        Console.WriteLine("Creating the first permission with the Flags " +
                          "property = ProtectData.");
        DataProtectionPermission sp1 = new DataProtectionPermission(
            DataProtectionPermissionFlags.ProtectData);

        Console.WriteLine("Creating the second permission with the Flags " +
                          "property = AllFlags.");

        DataProtectionPermission sp2 = new DataProtectionPermission(
            DataProtectionPermissionFlags.AllFlags);

        Console.WriteLine("Creating the third permission with a permission " +
                          "state = Unrestricted.");
        //<Snippet9>
        DataProtectionPermission sp3 = new DataProtectionPermission(
            PermissionState.Unrestricted);

        //</Snippet9>
        Console.WriteLine("Creating the fourth permission with a permission" +
                          " state = None.");

        DataProtectionPermission sp4 = new DataProtectionPermission(
            PermissionState.None);
        //<Snippet3>
        bool rc = sp2.IsSubsetOf(sp3);

        Console.WriteLine("Is the permission with all flags set (AllFlags) " +
                          "a subset of \n \tthe permission with an Unrestricted " +
                          "permission state? " + (rc ? "Yes" : "No"));
        rc = sp1.IsSubsetOf(sp2);
        Console.WriteLine("Is the permission with ProtectData access a " +
                          "subset of the permission with \n" + "\tAllFlags set? " +
                          (rc ? "Yes" : "No"));
        //</Snippet3>
        //<Snippet4>
        rc = sp3.IsUnrestricted();
        Console.WriteLine("Is the third permission unrestricted? " +
                          (rc ? "Yes" : "No"));
        //</Snippet4>
        //<Snippet5>
        Console.WriteLine("Copying the second permission to the fourth " +
                          "permission.");
        sp4 = (DataProtectionPermission)sp2.Copy();
        rc  = sp4.Equals(sp2);
        Console.WriteLine("Is the fourth permission equal to the second " +
                          "permission? " + (rc ? "Yes" : "No"));
        //</Snippet5>
        //<Snippet10>
        Console.WriteLine("Creating the intersection of the second and " +
                          "first permissions.");
        sp4 = (DataProtectionPermission)sp2.Intersect(sp1);
        Console.WriteLine("The value of the Flags property is: " +
                          sp4.Flags.ToString());
        //</Snippet10>
        //<Snippet6>
        Console.WriteLine("Creating the union of the second and first " +
                          "permissions.");
        sp4 = (DataProtectionPermission)sp2.Union(sp1);
        Console.WriteLine("Result of the union of the second permission " +
                          "with the first: " + sp4.Flags);
        //</Snippet6>
        //<Snippet7>
        Console.WriteLine("Using an XML round trip to reset the fourth " +
                          "permission.");
        sp4.FromXml(sp2.ToXml());
        rc = sp4.Equals(sp2);
        Console.WriteLine("Does the XML round trip result equal the " +
                          "original permission? " + (rc ? "Yes" : "No"));
        //</Snippet7>
    }