Share via


RSACryptoServiceProvider.UseMachineKeyStore Özellik

Tanım

Anahtarın kullanıcı profili deposu yerine bilgisayarın anahtar deposunda kalıcı olup olmayacağını belirten bir değer alır veya ayarlar.

public:
 static property bool UseMachineKeyStore { bool get(); void set(bool value); };
public static bool UseMachineKeyStore { get; set; }
static member UseMachineKeyStore : bool with get, set
Public Shared Property UseMachineKeyStore As Boolean

Özellik Değeri

true anahtarın bilgisayar anahtarı deposunda kalıcı olması gerekiyorsa; aksi takdirde , false.

Örnekler

Aşağıdaki kod örneği bir RSACryptoServiceProvider nesnesi oluşturur ve statik UseMachineKeyStore özelliği kullanıcı profili anahtar deposu yerine makine anahtar depounu kullanacak şekilde ayarlar.

using namespace System;
using namespace System::Security::Cryptography;

ref class RSAKeyStoreSample
{
public:
    static void Main()
    {
        // Set the static UseMachineKeyStore property to use the machine key
        // store instead of the user profile key store. All CSP instances not
        // initialized with CspParameters will use this setting.
        RSACryptoServiceProvider::UseMachineKeyStore = true;
        try
        {
            RSACryptoServiceProvider^ RSAalg;

            // This CSP instance will use the Machine Store as set above and is
            // initialized with no parameters.
            RSAalg = gcnew RSACryptoServiceProvider();
            ShowContainerInfo(RSAalg->CspKeyContainerInfo);
            RSAalg->PersistKeyInCsp = false;
            delete RSAalg;

            CspParameters^ cspParams = gcnew CspParameters();

            cspParams->KeyContainerName = "MyKeyContainer";

            // This CSP instance will use the User Store since cspParams are used.
            RSAalg = gcnew RSACryptoServiceProvider(cspParams);
            ShowContainerInfo(RSAalg->CspKeyContainerInfo);
            RSAalg->PersistKeyInCsp = false;
            delete RSAalg;

            cspParams->Flags |= CspProviderFlags::UseMachineKeyStore;

            // This CSP instance will use the Machine Store. Although cspParams are used,
            // the cspParams.Flags is set to CspProviderFlags.UseMachineKeyStore.
            RSAalg = gcnew RSACryptoServiceProvider(cspParams);
            ShowContainerInfo(RSAalg->CspKeyContainerInfo);
            RSAalg->PersistKeyInCsp = false;
            delete RSAalg;
        }
        catch (CryptographicException^ e)
        {
            Console::WriteLine("Exception: {0}", e->GetType()->FullName);
            Console::WriteLine(e->Message);
        }
    }

    static void ShowContainerInfo(CspKeyContainerInfo^ containerInfo)
    {
        String^ keyStore;

        Console::WriteLine();
        if (containerInfo->MachineKeyStore)
        {
            keyStore = "Machine Store";
        }
        else
        {
            keyStore = "User Store";
        }
        Console::WriteLine("Key Store:     {0}", keyStore);
        Console::WriteLine("Key Provider:  {0}", containerInfo->ProviderName);
        Console::WriteLine("Key Container: \"{0}\"", containerInfo->KeyContainerName);
        Console::WriteLine("Generated:     {0}", containerInfo->RandomlyGenerated);
        Console::WriteLine("Key Nubmer:    {0}", containerInfo->KeyNumber);
        Console::WriteLine("Removable Key: {0}", containerInfo->Removable);
    }
};

int main()
{
    RSAKeyStoreSample::Main();
}
using System;
using System.Security.Cryptography;

public class RSAKeyStoreSample
{
    public static void Main()
    {
        // Set the static UseMachineKeyStore property to use the machine key
        // store instead of the user profile key store. All CSP instances not
        // initialized with CspParameters will use this setting.
        RSACryptoServiceProvider.UseMachineKeyStore = true;
        try
        {
            // This CSP instance will use the Machine Store as set above and is
            // initialized with no parameters.
            using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider())
            {
                ShowContainerInfo(RSAalg.CspKeyContainerInfo);
                RSAalg.PersistKeyInCsp = false;
            }

            CspParameters cspParams = new CspParameters();

            cspParams.KeyContainerName = "MyKeyContainer";

            // This CSP instance will use the User Store since cspParams are used.
            using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams))
            {
                ShowContainerInfo(RSAalg.CspKeyContainerInfo);
                RSAalg.PersistKeyInCsp = false;
            }

            cspParams.Flags |= CspProviderFlags.UseMachineKeyStore;

            // This CSP instance will use the Machine Store. Although cspParams are used,
            // the cspParams.Flags is set to CspProviderFlags.UseMachineKeyStore.
            using (RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider(cspParams))
            {
                ShowContainerInfo(RSAalg.CspKeyContainerInfo);
                RSAalg.PersistKeyInCsp = false;
            }
        }
        catch (CryptographicException e)
        {
            Console.WriteLine("Exception: {0}", e.GetType().FullName);
            Console.WriteLine(e.Message);
        }
    }

    public static void ShowContainerInfo(CspKeyContainerInfo containerInfo)
    {
        string keyStore;

        Console.WriteLine();
        if (containerInfo.MachineKeyStore)
        {
            keyStore = "Machine Store";
        }
        else
        {
            keyStore = "User Store";
        }
        Console.WriteLine("Key Store:     {0}", keyStore);
        Console.WriteLine("Key Provider:  {0}", containerInfo.ProviderName);
        Console.WriteLine("Key Container: \"{0}\"", containerInfo.KeyContainerName);
        Console.WriteLine("Generated:     {0}", containerInfo.RandomlyGenerated);
        Console.WriteLine("Key Nubmer:    {0}", containerInfo.KeyNumber);
        Console.WriteLine("Removable Key: {0}", containerInfo.Removable);
    }
}
Imports System.Security.Cryptography

Public Class RSAKeyStoreSample
    Public Shared Sub Main()
        ' Set the static UseMachineKeyStore property to use the machine key
        ' store instead of the user profile key store. All CSP instances not
        ' initialized with CspParameters will use this setting.
        RSACryptoServiceProvider.UseMachineKeyStore = True
        Try
            ' This CSP instance will use the Machine Store as set above and is
            ' initialized with no parameters.
            Using RSAalg As New RSACryptoServiceProvider()
                ShowContainerInfo(RSAalg.CspKeyContainerInfo)
                RSAalg.PersistKeyInCsp = False
            End Using

            Dim cspParams As New CspParameters()

            cspParams.KeyContainerName = "MyKeyContainer"

            ' This CSP instance will use the User Store since cspParams are used.
            Using RSAalg As New RSACryptoServiceProvider(cspParams)
                ShowContainerInfo(RSAalg.CspKeyContainerInfo)
                RSAalg.PersistKeyInCsp = False
            End Using

            cspParams.Flags = cspParams.Flags Or CspProviderFlags.UseMachineKeyStore

            ' This CSP instance will use the Machine Store. Although cspParams are used,
            ' the cspParams.Flags is set to CspProviderFlags.UseMachineKeyStore.
            Using RSAalg As New RSACryptoServiceProvider(cspParams)
                ShowContainerInfo(RSAalg.CspKeyContainerInfo)
                RSAalg.PersistKeyInCsp = False
            End Using
        Catch e As CryptographicException
            Console.WriteLine("Exception: {0}", e.GetType().FullName)
            Console.WriteLine(e.Message)

        End Try
    End Sub

    Public Shared Sub ShowContainerInfo(containerInfo As CspKeyContainerInfo)
        Dim keyStore As String

        Console.WriteLine()
        If containerInfo.MachineKeyStore Then
            keyStore = "Machine Store"
        Else
            keyStore = "User Store"
        End If
        Console.WriteLine("Key Store:     {0}", keyStore)
        Console.WriteLine("Key Provider:  {0}", containerInfo.ProviderName)
        Console.WriteLine("Key Container: ""{0}""", containerInfo.KeyContainerName)
        Console.WriteLine("Generated:     {0}", containerInfo.RandomlyGenerated)
        Console.WriteLine("Key Nubmer:    {0}", containerInfo.KeyNumber)
        Console.WriteLine("Removable Key: {0}", containerInfo.Removable)
    End Sub
End Class

Açıklamalar

Bu özelliğin olarak true ayarlanması, bayrağın bir CspParameters nesneye geçirilmesiyle UseMachineKeyStore eşdeğerdir. UseMachineKeyStore özelliği geçerli uygulama etki alanındaki tüm kodlara uygulanırkenCspParameters, nesne yalnızca açıkça başvuran sınıflara uygulanır. Bu ayarlar, kullanıcı profili yüklenmeyen bir hesabın kimliğine bürünme veya bu hesabın altında çalışırken kullanışlıdır. Ayar UseMachineKeyStore , anahtar deposu konumunu yalnızca parametre olmadan başlatılırsa RSACryptoServiceProvider etkiler.

Şunlara uygulanır

Ayrıca bkz.