Export (0) Print
Expand All

RSACryptoServiceProvider.UseMachineKeyStore Property

Gets or sets a value indicating whether the key should be persisted in the computer's key store instead of the user profile store.

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

public:
static property bool UseMachineKeyStore {
	bool get ();
	void set (bool value);
}
/** @property */
public static boolean get_UseMachineKeyStore ()

/** @property */
public static void set_UseMachineKeyStore (boolean value)

public static function get UseMachineKeyStore () : boolean

public static function set UseMachineKeyStore (value : boolean)

Not applicable.

Property Value

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

Setting this property to true is equivalent to passing the UseMachineKeyStore flag to a CspParameters object. The UseMachineKeyStore property applies to all code in the current application domain, while the CspParameters object applies only to classes that explicitly reference it. These settings are useful when impersonating or running under an account whose user profile is not loaded.

The following code example creates an RSACryptoServiceProvider object and sets the static UseMachineKeyStore property to use the machine key store instead of the user profile key store.

using namespace System;
using namespace System::Security::Cryptography;
void RSAPersistKeyInCSP( String^ ContainerName )
{
   try
   {
      
      // Create a new instance of CspParameters.  Pass
      // 13 to specify a RSA 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 RSA 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";
   
   //Set the static UseMachineKeyStore property to
   //use the machine key store instead of the user
   //profile key store.  All code in the current 
   //application domain will use this setting.
   RSACryptoServiceProvider::UseMachineKeyStore = true;
   
   //Create a new key and persist it in 
   //the key container.  
   RSAPersistKeyInCSP( KeyContainerName );
   
   //Delete the key from the key container.
   RSADeleteKeyInCSP( KeyContainerName );
}


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

class RSACSPSample
{
    public static void main(String args[])
    {
        String keyContainerName = "MyKeyContainer";
        //Set the static UseMachineKeyStore property to
        //use the machine key store instead of the user
        //profile key store.  All code in the current 
        //application domain will use this setting.
        RSACryptoServiceProvider.set_UseMachineKeyStore(true);
        //Create a new key and persist it in 
        //the key container.  
        RSAPersistKeyInCSP(keyContainerName);
        //Delete the key from the key container.
        RSADeleteKeyInCSP(keyContainerName);
    } //main

    public static void RSAPersistKeyInCSP(String containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a RSA 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.get_Message());
        }
    } //RSAPersistKeyInCSP

    public static void RSADeleteKeyInCSP(String containerName)
    {
        try {
            // Create a new instance of CspParameters.  Pass
            // 13 to specify a RSA 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.set_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.get_Message());
        }
    } //RSADeleteKeyInCSP
} //RSACSPSample

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

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft