Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RSACryptoServiceProvider::PersistKeyInCsp Property

 

Gets or sets a value indicating whether the key should be persisted in the cryptographic service provider (CSP).

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

public:
property bool PersistKeyInCsp {
	bool get();
	void set(bool value);
}

Property Value

Type: System::Boolean

true if the key should be persisted in the CSP; otherwise, false.

Use this property to persist a key in a key container.

The PersistKeyInCsp property is automatically set to true when you specify a key container name in the KeyContainerName field of a CspParameters object and use it to initialize an RSACryptoServiceProvider object by calling one of the constructors with a parameters parameter.

The PersistKeyInCsp property has no effect if the RSACryptoServiceProvider object is created with a null key container name.

The following code example creates an RSACryptoServiceProvider object and persists the key to a key container.

using namespace System;
using namespace System::Security::Cryptography;
void RSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {

      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;

      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;

      //Create a new instance of RSACryptoServiceProvider to generate
      //a new key pair.  Pass the CspParameters class to persist the 
      //key in the container.  The PersistKeyInCsp property is true by 
      //default, allowing the key to be persisted. 
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );

      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was persisted in the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

void RSADeleteKeyInCSP( String^ ContainerName )
{
   try
   {

      // Create a new instance of CspParameters.  Pass
      // 13 to specify a DSA container or 1 to specify
      // an RSA container.  The default is 1.
      CspParameters^ cspParams = gcnew CspParameters;

      // Specify the container name using the passed variable.
      cspParams->KeyContainerName = ContainerName;

      //Create a new instance of RSACryptoServiceProvider. 
      //Pass the CspParameters class to use the 
      //key in the container.
      RSACryptoServiceProvider^ RSAalg = gcnew RSACryptoServiceProvider( cspParams );

      //Explicitly set the PersistKeyInCsp property to false
      //to delete the key entry in the container.
      RSAalg->PersistKeyInCsp = false;

      //Call Clear to release resources and delete the key from the container.
      RSAalg->Clear();

      //Indicate that the key was persisted.
      Console::WriteLine( "The RSA key was deleted from the container, \"{0}\".", ContainerName );
   }
   catch ( CryptographicException^ e ) 
   {
      Console::WriteLine( e->Message );
   }

}

int main()
{
   String^ KeyContainerName = "MyKeyContainer";

   //Create a new key and persist it in 
   //the key container.  
   RSAPersistKeyInCSP( KeyContainerName );

   //Delete the key from the key container.
   RSADeleteKeyInCSP( KeyContainerName );
}

KeyContainerPermissionAccessEntryCollection

for permission to delete a key. Associated enumeration: KeyContainerPermissionFlags::Delete

-or-

for permission to create a key. Associated enumeration: KeyContainerPermissionFlags::Create

.NET Framework
Available since 1.1
Windows Phone Silverlight
Available since 7.1
Return to top
Show:
© 2017 Microsoft