Export (0) Print
Expand All

KeyContainerPermissionAttribute Class

Note: This class is new in the .NET Framework version 2.0.

Allows security actions for KeyContainerPermission to be applied to code using declarative security. This class cannot be inherited.

Namespace: System.Security.Permissions
Assembly: mscorlib (in mscorlib.dll)

[SerializableAttribute] 
[AttributeUsageAttribute(AttributeTargets::Assembly|AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Constructor|AttributeTargets::Method, AllowMultiple=true, Inherited=false)] 
[ComVisibleAttribute(true)] 
public ref class KeyContainerPermissionAttribute sealed : public CodeAccessSecurityAttribute
/** @attribute SerializableAttribute() */ 
/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false) */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class KeyContainerPermissionAttribute extends CodeAccessSecurityAttribute
SerializableAttribute 
AttributeUsageAttribute(AttributeTargets.Assembly|AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Constructor|AttributeTargets.Method, AllowMultiple=true, Inherited=false) 
ComVisibleAttribute(true) 
public final class KeyContainerPermissionAttribute extends CodeAccessSecurityAttribute

The scope of the declaration that is allowed depends on the SecurityAction value that is used.

The security information declared by a security attribute is stored in the metadata of the attribute target and is accessed by the system at run time. KeyContainerPermissionAttribute is used only for declarative security. For imperative security, use the KeyContainerPermission class.

The following code example shows the use of members of the KeyContainerPermissionAttribute class.

// The following commented code should be compiled as a console application.  The application 
// executes the class library code example showing the use of the KeyContainerPermissionAttribute.
//#using <mscorlib.dll>
//using namespace System;
//using namespace System::Security;
//using namespace System::Security::Permissions;
//using namespace System::Security::Cryptography;
//#using <KeyContainerPermissionAttribute.dll>
//
//// Tests the KeyContainerPermissionAttribute.
//[STAThread]
//int main()
//{
//    CspParameters* cspParams = new CspParameters();
//    cspParams->KeyContainerName = S"MyKeyContainer";
//    RSACryptoServiceProvider* rsa = new RSACryptoServiceProvider(cspParams);
//
//    // Display the key information to the console.
//    Console::WriteLine(S"Key added to container: \n  {0}", rsa->ToXmlString(true));
//    KeyContainerUtil* keyCU = new KeyContainerUtil();
//    // The following call creates a key container with the specified name if one doesn't exist.
//    // In this case the key container does exist.
//    keyCU->GenKey_SaveInContainer(S"MyKeyContainer");
//    // The following call deletes the key container.
//    keyCU->DeleteKeyFromContainer(S"MyKeyContainer");
//    // The next call to create a key container fails because the library
//    // routine does not have the permissions to create a key container.
//    keyCU->GenKey_SaveInContainer(S"MyKeyContainer");
//    Console::WriteLine(S"Press the Enter key to exit.");
//    Console::ReadKey();
//
//}
#using <System.dll>

using namespace System;
using namespace System::Security;
using namespace System::Security::Permissions;
using namespace System::Security::Cryptography;

// Prohibit the creation of a new key container.
[assembly:KeyContainerPermissionAttribute(SecurityAction::RequestRefuse,Flags=KeyContainerPermissionFlags::Create,
KeyContainerName="MyKeyContainer",KeyStore="User",
ProviderName="Microsoft Strong Cryptographic Provider")];

public ref class KeyContainerUtil
{
public:
   KeyContainerUtil(){}


   // The following method throws an exception if the containerName parameter identifies a 
   // new container.  Otherwise the method executes correctly.
   void GenKey_SaveInContainer( String^ containerName )
   {
      try
      {
         
         // Create the CspParameters object and set the name of the key  
         // container used to store the RSA key pair.
         CspParameters^ cspParams = gcnew CspParameters;
         cspParams->KeyContainerName = containerName;
         
         // Create a new instance of RSACryptoServiceProvider that accesses
         // the key container specified by the containerName parameter.
         RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider( cspParams );
         
         // Display the key information to the console.
         Console::WriteLine( "Key added to container: \n  {0}", rsa->ToXmlString( true ) );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception thrown: {0}", e->Message );
      }

   }

   void DeleteKeyFromContainer( String^ containerName )
   {
      try
      {
         
         // Create the CspParameters object and set the name of the key container 
         // used to store the RSA key pair, then delete the key entry in the container.
         CspParameters^ cspParams = gcnew CspParameters;
         cspParams->KeyContainerName = containerName;
         
         // Create a new instance of RSACryptoServiceProvider that accesses
         // the key container MyKeycontainerName.
         RSACryptoServiceProvider^ rsa = gcnew RSACryptoServiceProvider( cspParams );
         
         // The following statement causes the key to be deleted by not persisting the key.
         rsa->PersistKeyInCsp = false;
         
         // Call Clear to release resources, deleting the key container.
         rsa->Clear();
         Console::WriteLine( "Key and container deleted." );
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception thrown when deleting key: {0}", e->Message );
      }

   }

};


package KeyContainerPermissionAttributeDemo ; 
// The following commented code should be compiled as a console application.  
// The application executes the class library code example showing the 
// use of the KeyContainerPermissionAttribute.
//    import System.*;
//    import System.Security.*;
//    import System.Security.Permissions.*;
//    import System.Security.Cryptography.*;
//    import KeyContainerPermissionAttributeDemo.*;
//    class AttributeTest
//    {
//        //Tests the KeyContainerPermissionAttribute.
//        /** @attribute STAThread()
//         */
//        static void main(String[] args)
//        {
//            CspParameters cspParams =  new CspParameters();
//            cspParams.keyContainerName = "MyKeyContainer";
//            RSACryptoServiceProvider rsa =  
//                new RSACryptoServiceProvider(cspParams);
//            // Display the key information to the console.
//            Console.WriteLine("Key added to container: \n  {0}", 
//                rsa.ToXmlString(true));
//            KeyContainerUtil keyCU =  new KeyContainerUtil();
//            // The following call creates a key container with the specified 
//            // name if one doesn't exist.
//            // In this case the key container does exist.
//            keyCU.GenKey_SaveInContainer("MyKeyContainer");
//            // The following call deletes the key container.
//            keyCU.DeleteKeyFromContainer("MyKeyContainer");
//            // The next call to create a key container fails because the 
//            // library routine does not have the permissions to create a key 
//            // container.
//            keyCU.GenKey_SaveInContainer("MyKeyContainer");
//            Console.WriteLine("Press the Enter key to exit.");
//            Console.ReadKey();
//        } //main 
//    } //AttributeTest

import System.*;
import System.Security.*;
import System.Security.Permissions.*;
import System.Security.Cryptography.*;

// Prohibit the creation of a new key container.
/** @assembly.class KeyContainerPermission(SecurityAction.RequestRefuse, 
    Flags = KeyContainerPermissionFlags.Create, 
    KeyContainerName = "MyKeyContainer", 
    KeyStore = "User", ProviderName = "Microsoft Strong Cryptographic Provider")
 */
public class KeyContainerUtil
{
    public KeyContainerUtil()
    {
    } //KeyContainerUtil

    // The following method throws an exception if the containerName 
    // parameter identifies a new container.  
    // Otherwise the method executes correctly.
    public void GenKey_SaveInContainer(String containerName)
    {
        try {
            // Create the CspParameters object and set the name of the key  
            // container used to store the RSA key pair.
            CspParameters cspParams = new CspParameters();
            cspParams.KeyContainerName = containerName;

            // Create a new instance of RSACryptoServiceProvider that accesses
            // the key container specified by the containerName parameter.
            RSACryptoServiceProvider rsa = 
                new RSACryptoServiceProvider(cspParams);

            // Display the key information to the console.
            Console.WriteLine("Key added to container: \n  {0}", 
                rsa.ToXmlString(true));
        }
        catch (System.Exception e) {
            Console.WriteLine(("Exception thrown: " + e.get_Message()));
        }
    } //GenKey_SaveInContainer

    public void DeleteKeyFromContainer(String containerName)
    {
        try {
            // Create the CspParameters object and set the name of the 
            // key container used to store the RSA key pair, then delete 
            // the key entry in the container.
            CspParameters cspParams = new CspParameters();
            cspParams.KeyContainerName = containerName;

            // Create a new instance of RSACryptoServiceProvider that accesses
            // the key container MyKeycontainerName.
            RSACryptoServiceProvider rsa = 
                new RSACryptoServiceProvider(cspParams);

            // The following statement causes the key to be deleted by 
            // not persisting the key.
            rsa.set_PersistKeyInCsp(false);

            // Call Clear to release resources, deleting the key container.
            rsa.Clear();
            Console.WriteLine("Key and container deleted.");
        }
        catch (System.Exception e) {
            Console.WriteLine(("Exception thrown when deleting key: " 
                + e.get_Message()));
        }
    } //DeleteKeyFromContainer
} //KeyContainerUtil   

System.Object
   System.Attribute
     System.Security.Permissions.SecurityAttribute
       System.Security.Permissions.CodeAccessSecurityAttribute
        System.Security.Permissions.KeyContainerPermissionAttribute

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft