Export (0) Print
Expand All

DSACryptoServiceProvider.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 */
public boolean get_PersistKeyInCsp ()

/** @property */
public void set_PersistKeyInCsp (boolean value)

public function get PersistKeyInCsp () : boolean

public function set PersistKeyInCsp (value : boolean)

Not applicable.

Property Value

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

Use this property when you want to persist a key in a key container.

The following code example creates an instance of DSACryptoServiceProvider, persists the key in a key container, and then deletes the key.

using System;
using System.Security.Cryptography;

class DSACSPSample
{

    static void Main()
    {

        string KeyContainerName = "MyKeyContainer";

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

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

    public static void DSAPersistKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an DSA 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 DSACryptoServiceProvider 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. 
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

          

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

        }
    }

    public static void DSADeleteKeyInCSP(string ContainerName)
    {
        try
        {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an DSA 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 DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            DSACryptoServiceProvider DSAalg = new DSACryptoServiceProvider(cspParams);

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

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

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

        }
    }
}

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

class DSACSPSample
{
    public static void main(String args[])
    {
        String keyContainerName = "MyKeyContainer";
        //Create a new key and persist it in 
        //the key container.  
        DSAPersistKeyInCSP(keyContainerName);
        //Delete the key from the key container.
        DSADeleteKeyInCSP(keyContainerName);
    } //main

    public static void DSAPersistKeyInCSP(String containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an DSA 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 DSACryptoServiceProvider 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. 
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the container, " 
                + "\"{0}\".", containerName);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //DSAPersistKeyInCSP

    public static void DSADeleteKeyInCSP(String containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a DSA container or 1 to specify
            // an DSA 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 DSACryptoServiceProvider. 
            //Pass the CspParameters class to use the 
            //key in the container.
            DSACryptoServiceProvider dsaAlg = 
                new DSACryptoServiceProvider(cspParams);
            //Explicitly set the PersistKeyInCsp property to false
            //to delete the key entry in the container.
            dsaAlg.set_PersistKeyInCsp(false);
            //Call Clear to release resources and delete the key from the 
            //container.
            dsaAlg.Clear();
            //Indicate that the key was persisted.
            Console.WriteLine("The DSA key was deleted from the container, " 
                + "\"{0}\".", containerName);
        }
        catch (CryptographicException e) {
            Console.WriteLine(e.get_Message());
        }
    } //DSADeleteKeyInCSP
} //DSACSPSample

  • KeyContainerPermission  for permission to delete a key. Associated enumeration: Delete. Security action: Demand. -or- for permission to create a key. Associated enumeration: Create. Security action: Demand.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft