Export (0) Print
Expand All

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 bool PersistKeyInCsp { get; set; }

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 a CspParameters object and use it to initialize an RSACryptoServiceProvider object. You can specify a container name using the KeyContainerName field.

If you set the PersistKeyInCsp property to true without initializing the RSACryptoServiceProvider object with a CspParameters object, a random key container name prepended with "CLR" is created.

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

using System;
using System.Security.Cryptography;


class RSACSPSample
{

    static void 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);
    }

    public static 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 = new 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 = new 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);

        }
    }

    public static 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 = new 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 = new 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);

        }
    }
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft