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); }
// 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> }