.NET Framework Class Library
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)
Syntax

Visual Basic (Declaration)
Public Property PersistKeyInCsp As Boolean
Visual Basic (Usage)
Dim instance As DSACryptoServiceProvider
Dim value As Boolean

value = instance.PersistKeyInCsp

instance.PersistKeyInCsp = value
C#
public bool PersistKeyInCsp { get; set; }
Visual C++
public:
property bool PersistKeyInCsp {
    bool get ();
    void set (bool value);
}
JScript
public function get PersistKeyInCsp () : boolean
public function set PersistKeyInCsp (value : boolean)

Property Value

Type: System..::.Boolean
true if the key should be persisted in the CSP; otherwise, false.
Remarks

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

Examples

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

Visual Basic
Imports System.Security.Cryptography

Module DSACSPExample

    Sub Main()

        Dim KeyContainerName As String = "MyKeyContainer"

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

        'Delete the key from the key container.
        DSADeleteKeyInCSP(KeyContainerName)
    End Sub


    Sub DSAPersistKeyInCSP(ByVal ContainerName As String)
        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.
            Dim cspParams As 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. 
            Dim DSAalg As New DSACryptoServiceProvider(cspParams)

            'Indicate that the key was persisted.
            Console.WriteLine("The DSA key was persisted in the container, ""{0}"".", ContainerName)
        Catch e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub


    Sub DSADeleteKeyInCSP(ByVal ContainerName As String)
        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.
            Dim cspParams As 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.
            Dim DSAalg As 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 e As CryptographicException
            Console.WriteLine(e.Message)
        End Try
    End Sub

End Module
C#
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);

        }
    }
}
Visual C++
using namespace System;
using namespace System::Security::Cryptography;
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 = gcnew 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 = gcnew 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 );
   }

}

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 = gcnew 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 = gcnew 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 );
   }

}

int 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 );
}

.NET Framework Security

Platforms

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

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0
See Also

Reference

Other Resources

Tags :


Page view tracker