コード例 #1
0
        private static void DenyOpen()
        {
            try
            {
                //<Snippet20>
                // Create a KeyContainerPermission with the right
                // to open the key container.
                KeyContainerPermission keyContainerPerm = new
                                                          KeyContainerPermission(KeyContainerPermissionFlags.Open);
                //</Snippet20>

                // Demonstrate the results of a deny for an open action.
                keyContainerPerm.Deny();

                // The next line causes an exception to be thrown when the
                // infrastructure code attempts to open the key container.
                CspKeyContainerInfo info = new CspKeyContainerInfo(cspParams);
            }
            catch (Exception e)
            {
                Console.WriteLine("Expected exception thrown: " + e.Message);
            }

            // Revert the deny.
            CodeAccessPermission.RevertDeny();
        }
コード例 #2
0
ファイル: form1.cs プロジェクト: zhamppx97/dotnet-api-docs
        void RunCode()
        {
            try
            {
                // Deny a permission.
                KeyContainerPermission kCP1 = new KeyContainerPermission(
                    KeyContainerPermissionFlags.Decrypt);
                kCP1.Deny();

                // Demand the denied permission and display the
                // exception properties.
                Display("Demanding a denied permission. \n\n");
                DemandDeniedPermission();
                Display("************************************************\n");
                CodeAccessPermission.RevertDeny();

                // Demand the permission refused in the
                // assembly-level attribute.
                Display("Demanding a refused permission. \n\n");
                DemandRefusedPermission();
                Display("************************************************\n");

                // Demand the permission implicitly refused through a
                // PermitOnly attribute. Permit only the permission that
                // will cause the failure and the security permissions
                // necessary to display the results of the failure.
                PermissionSet permitOnly = new PermissionSet(
                    PermissionState.None);
                permitOnly.AddPermission(new KeyContainerPermission(
                                             KeyContainerPermissionFlags.Import));
                permitOnly.AddPermission(new SecurityPermission(
                                             SecurityPermissionFlag.ControlEvidence |
                                             SecurityPermissionFlag.ControlPolicy |
                                             SecurityPermissionFlag.SerializationFormatter));
                permitOnly.PermitOnly();
                Display("Demanding an implicitly refused permission. \n\n");
                DemandPermitOnly();
            }
            catch (Exception sE)
            {
                Display("************************************************\n");
                //<Snippet17>
                Display("Displaying an exception using the ToString method: ");
                Display(sE.ToString());
                //</Snippet17>
            }
        }
コード例 #3
0
        private static void DeleteContainer()
        {
            try
            {
                // Create a KeyContainerPermission with the right to
                // create a key container.
                KeyContainerPermission keyContainerPerm = new
                                                          KeyContainerPermission(KeyContainerPermissionFlags.Create);

                // Deny the ability to create a key container.
                // This deny is used to show the key container has been
                // successfully deleted.
                keyContainerPerm.Deny();

                // Retrieve the key from the container.
                // This code executes successfully because the key
                // container already exists.
                // The deny for permission to create a key container
                // does not affect this method call.
                GetKeyFromContainer("MyKeyContainer");

                // Delete the key and the container.
                DeleteKeyContainer("MyKeyContainer");

                // Attempt to obtain the key from the deleted key container.
                // This time the method call results in an exception because of
                // an attempt to create a new key container.
                Console.WriteLine("\nAttempt to create a new key container " +
                                  "with create permission denied.");
                GetKeyFromContainer("MyKeyContainer");
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("Expected exception thrown: " + e.Message);
            }
        }