X509Store 클래스

정의

인증서가 유지 및 관리되는 실제 저장소인 X.509 저장소를 나타냅니다.Represents an X.509 store, which is a physical store where certificates are persisted and managed. 이 클래스는 상속될 수 없습니다.This class cannot be inherited.

public ref class X509Store sealed : IDisposable
public sealed class X509Store : IDisposable
type X509Store = class
    interface IDisposable
Public NotInheritable Class X509Store
Implements IDisposable
상속
X509Store
구현

예제

이 섹션에서는 두 가지 예제를 포함합니다.This section contains two examples. 첫 번째 예에서는 각 표준 X.509 저장소 및 인증서 수가 목록을 열 수는 방법을 보여 줍니다.The first example demonstrates how you can open standard X.509 stores and list the number of certificates in each.

두 번째 예제에는 추가 단일 인증서 및 인증서의 범위를 제거 하는 방법을 보여 줍니다.The second example demonstrates how you can add and remove single certificates and ranges of certificates.

예제 1Example 1

이 예제에서는 현재 컴퓨터의 각 표준 위치에 있는 각 표준 저장소를 열려고 시도 합니다.This example tries to open each standard store in each standard location on the current computer. 여부를 각 저장소 존재 하 고를 수가 인증서 따라서 있는지를 보여 주는 요약이 표시 됩니다.It prints a summary that shows whether each store exists and, if so, the number of certificates it contains.

이 예에서는 만듭니다는 X509Store 표준 이름과 표준 위치의 각 조합에 대 한 개체입니다.The example creates an X509Store object for each combination of standard name and standard location. 호출 합니다 Open 메서드를 OpenFlags.OpenExistingOnly 플래그를 이미 존재 하는 경우에 있는 물리적 저장소를 엽니다.It calls the Open method with the OpenFlags.OpenExistingOnly flag, which opens the physical store only if it already exists. 예제에서는 물리적 저장소 있으면 합니다 Name, Location, 및 Certificates 속성을 저장소에 인증서의 수를 표시 합니다.If the physical store exists, the example uses the Name, Location, and Certificates properties to display the number of certificates in the store.

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;

public class Example
{
    static void Main()
    {
        Console.WriteLine("\r\nExists Certs Name and Location");
        Console.WriteLine("------ ----- -------------------------");

        foreach (StoreLocation storeLocation in (StoreLocation[]) 
            Enum.GetValues(typeof(StoreLocation)))
        {
            foreach (StoreName storeName in (StoreName[]) 
                Enum.GetValues(typeof(StoreName)))
            {
                X509Store store = new X509Store(storeName, storeLocation);

                try
                {
                    store.Open(OpenFlags.OpenExistingOnly);

                    Console.WriteLine("Yes    {0,4}  {1}, {2}", 
                        store.Certificates.Count, store.Name, store.Location);
                }   
                catch (CryptographicException)
                {
                    Console.WriteLine("No           {0}, {1}", 
                        store.Name, store.Location);
                }
            }
            Console.WriteLine();
        }
    }
}

/* This example produces output similar to the following:

Exists Certs Name and Location
------ ----- -------------------------
Yes       1  AddressBook, CurrentUser
Yes      25  AuthRoot, CurrentUser
Yes     136  CA, CurrentUser
Yes      55  Disallowed, CurrentUser
Yes      20  My, CurrentUser
Yes      36  Root, CurrentUser
Yes       0  TrustedPeople, CurrentUser
Yes       1  TrustedPublisher, CurrentUser

No           AddressBook, LocalMachine
Yes      25  AuthRoot, LocalMachine
Yes     131  CA, LocalMachine
Yes      55  Disallowed, LocalMachine
Yes       3  My, LocalMachine
Yes      36  Root, LocalMachine
Yes       0  TrustedPeople, LocalMachine
Yes       1  TrustedPublisher, LocalMachine

 */
Option Strict On

Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates

Module Example

    Sub Main()

        Console.WriteLine(vbCrLf & "Exists Certs Name and Location")
        Console.WriteLine("------ ----- -------------------------")

        For Each storeLocation As StoreLocation In _
                CType([Enum].GetValues(GetType(StoreLocation)), StoreLocation())

            For Each storeName As StoreName In _
                    CType([Enum].GetValues(GetType(StoreName)), StoreName())

                Dim store As New X509Store(StoreName, StoreLocation)

                Try
                    store.Open(OpenFlags.OpenExistingOnly)
                    Console.WriteLine("Yes    {0,4}  {1}, {2}", _
                        store.Certificates.Count, store.Name, store.Location)
                Catch e As CryptographicException
                    Console.WriteLine("No           {0}, {1}", _
                        store.Name, store.Location)
                End Try
            Next

            Console.WriteLine()
        Next
    End Sub
End Module

' This example produces output similar to the following:

'Exists Certs Name and Location
'------ ----- -------------------------
'Yes       1  AddressBook, CurrentUser
'Yes      25  AuthRoot, CurrentUser
'Yes     136  CA, CurrentUser
'Yes      55  Disallowed, CurrentUser
'Yes      20  My, CurrentUser
'Yes      36  Root, CurrentUser
'Yes       0  TrustedPeople, CurrentUser
'Yes       1  TrustedPublisher, CurrentUser

'No           AddressBook, LocalMachine
'Yes      25  AuthRoot, LocalMachine
'Yes     131  CA, LocalMachine
'Yes      55  Disallowed, LocalMachine
'Yes       3  My, LocalMachine
'Yes      36  Root, LocalMachine
'Yes       0  TrustedPeople, LocalMachine
'Yes       1  TrustedPublisher, LocalMachine

예제 2Example 2

이 예제에서는 X.509 인증서 저장소, 추가 및 인증서를 삭제 합니다. 열리고 저장소를 닫습니다.This example opens an X.509 certificate store, adds and deletes certificates, and then closes the store. 인증서를 추가 하 고 로컬 저장소에서 제거할가 세 개 있다고 가정 합니다.It assumes that you have three certificates to add to and remove from a local store.

#using <System.dll>
#using <System.Security.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Cryptography::X509Certificates;
using namespace System::IO;
int main()
{
   
   //Create new X509 store called teststore from the local certificate store.
   X509Store ^ store = gcnew X509Store( "teststore",StoreLocation::CurrentUser );
   store->Open( OpenFlags::ReadWrite );
   X509Certificate2 ^ certificate = gcnew X509Certificate2;
   
   //Create certificates from certificate files.
   //You must put in a valid path to three certificates in the following constructors.
   X509Certificate2 ^ certificate1 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" );
   X509Certificate2 ^ certificate2 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" );
   X509Certificate2 ^ certificate5 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" );
   
   //Create a collection and add two of the certificates.
   X509Certificate2Collection ^ collection = gcnew X509Certificate2Collection;
   collection->Add( certificate2 );
   collection->Add( certificate5 );
   
   //Add certificates to the store.
   store->Add( certificate1 );
   store->AddRange( collection );
   X509Certificate2Collection ^ storecollection = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   Console::WriteLine( "Store name: {0}", store->Name );
   Console::WriteLine( "Store location: {0}", store->Location );
   System::Collections::IEnumerator^ myEnum = storecollection->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum->Current);
      Console::WriteLine( "certificate name: {0}", x509->Subject );
   }

   
   //Remove a certificate.
   store->Remove( certificate1 );
   X509Certificate2Collection ^ storecollection2 = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   Console::WriteLine( "{1}Store name: {0}", store->Name, Environment::NewLine );
   System::Collections::IEnumerator^ myEnum1 = storecollection2->GetEnumerator();
   while ( myEnum1->MoveNext() )
   {
      X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum1->Current);
      Console::WriteLine( "certificate name: {0}", x509->Subject );
   }

   
   //Remove a range of certificates.
   store->RemoveRange( collection );
   X509Certificate2Collection ^ storecollection3 = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
   Console::WriteLine( "{1}Store name: {0}", store->Name, Environment::NewLine );
   if ( storecollection3->Count == 0 )
   {
      Console::WriteLine( "Store contains no certificates." );
   }
   else
   {
      System::Collections::IEnumerator^ myEnum2 = storecollection3->GetEnumerator();
      while ( myEnum2->MoveNext() )
      {
         X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum2->Current);
         Console::WriteLine( "certificate name: {0}", x509->Subject );
      }
   }

   
   //Close the store.
   store->Close();
}

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.IO;

public class X509store2
{
    public static void Main (string[] args)
    {
        //Create new X509 store called teststore from the local certificate store.
        X509Store store = new X509Store ("teststore", StoreLocation.CurrentUser);
        store.Open (OpenFlags.ReadWrite);
        X509Certificate2 certificate = new X509Certificate2 ();

        //Create certificates from certificate files.
        //You must put in a valid path to three certificates in the following constructors.
        X509Certificate2 certificate1 = new X509Certificate2 ("c:\\mycerts\\*****.cer");
        X509Certificate2 certificate2 = new X509Certificate2 ("c:\\mycerts\\*****.cer");
        X509Certificate2 certificate5 = new X509Certificate2 ("c:\\mycerts\\*****.cer");

        //Create a collection and add two of the certificates.
        X509Certificate2Collection collection = new X509Certificate2Collection ();
        collection.Add (certificate2);
        collection.Add (certificate5);

        //Add certificates to the store.
        store.Add (certificate1);
        store.AddRange (collection);

        X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates;
        Console.WriteLine ("Store name: {0}", store.Name);
        Console.WriteLine ("Store location: {0}", store.Location);
        foreach (X509Certificate2 x509 in storecollection)
        {
            Console.WriteLine("certificate name: {0}",x509.Subject);
        }

        //Remove a certificate.
        store.Remove (certificate1);
        X509Certificate2Collection storecollection2 = (X509Certificate2Collection)store.Certificates;
        Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine);
        foreach (X509Certificate2 x509 in storecollection2)
        {
            Console.WriteLine ("certificate name: {0}", x509.Subject);
        }

        //Remove a range of certificates.
        store.RemoveRange (collection);
        X509Certificate2Collection storecollection3 = (X509Certificate2Collection)store.Certificates;
        Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine);
        if (storecollection3.Count == 0)
        {
            Console.WriteLine ("Store contains no certificates.");
        }
        else
        {
            foreach (X509Certificate2 x509 in storecollection3)
            {
                Console.WriteLine ("certificate name: {0}", x509.Subject);
            }
        }

        //Close the store.
        store.Close ();
    }	
}
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.IO



Class X509store2

    Shared Sub Main(ByVal args() As String)
        'Create new X509 store called teststore from the local certificate store.
        Dim store As New X509Store("teststore", StoreLocation.CurrentUser)
        store.Open(OpenFlags.ReadWrite)
        Dim certificate As New X509Certificate2()

        'Create certificates from certificate files.
        'You must put in a valid path to three certificates in the following constructors.
        Dim certificate1 As New X509Certificate2("c:\mycerts\*****.cer")
        Dim certificate2 As New X509Certificate2("c:\mycerts\*****.cer")
        Dim certificate5 As New X509Certificate2("c:\mycerts\*****.cer")

        'Create a collection and add two of the certificates.
        Dim collection As New X509Certificate2Collection()
        collection.Add(certificate2)
        collection.Add(certificate5)

        'Add certificates to the store.
        store.Add(certificate1)
        store.AddRange(collection)

        Dim storecollection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("Store name: {0}", store.Name)
        Console.WriteLine("Store location: {0}", store.Location)
        Dim x509 As X509Certificate2
        For Each x509 In storecollection
            Console.WriteLine("certificate name: {0}", x509.Subject)
        Next x509

        'Remove a certificate.
        store.Remove(certificate1)
        Dim storecollection2 As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("{1}Store name: {0}", store.Name, Environment.NewLine)
        Dim x509a As X509Certificate2
        For Each x509a In storecollection2
            Console.WriteLine("certificate name: {0}", x509a.Subject)
        Next x509a

        'Remove a range of certificates.
        store.RemoveRange(collection)
        Dim storecollection3 As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("{1}Store name: {0}", store.Name, Environment.NewLine)
        If storecollection3.Count = 0 Then
            Console.WriteLine("Store contains no certificates.")
        Else
            Dim x509b As X509Certificate2
            For Each x509b In storecollection3
                Console.WriteLine("certificate name: {0}", x509b.Subject)
            Next x509b
        End If

        'Close the store.
        store.Close()

    End Sub
End Class

설명

X.509 저장소를 사용 하려면이 클래스를 사용 합니다.Use this class to work with an X.509 store.

중요

부터 합니다 .NET Framework 4.6.NET Framework 4.6이 구현 입력는 IDisposable 인터페이스입니다.Starting with the .NET Framework 4.6.NET Framework 4.6, this type implements the IDisposable interface. 형식을 사용 하 여 마쳤으면 직접 또는 간접적으로의 삭제 해야 있습니다.When you have finished using the type, you should dispose of it either directly or indirectly. 직접 형식의 dispose 호출 해당 Dispose 의 메서드를 try / catch 블록입니다.To dispose of the type directly, call its Dispose method in a try/catch block. 삭제 하지 직접, 언어 구문 같은 사용 using (C#에서) 또는 Using (Visual Basic에서는).To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). 자세한 내용은 "를 사용 하는 개체는 구현 IDisposable" 섹션을 참조 하세요.를 IDisposable 인터페이스 항목입니다.For more information, see the "Using an Object that Implements IDisposable" section in the IDisposable interface topic.

대상으로 하는 앱에 대 한는 .NET Framework 4.5.2.NET Framework 4.5.2 및 이전 버전의 X509Store 클래스를 구현 하지 않습니다는 IDisposable 인터페이스 없고 따라서를 Dispose 메서드.For apps that target the .NET Framework 4.5.2.NET Framework 4.5.2 and earlier versions, the X509Store class does not implement the IDisposable interface and therefore does not have a Dispose method.

생성자

X509Store()

현재 사용자 저장소의 개인 인증서를 사용하여 X509Store 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using the personal certificates of the current user store.

X509Store(IntPtr)

X509Store 저장소에 대한 Intptr 핸들을 사용하여 HCERTSTORE 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using an Intptr handle to an HCERTSTORE store.

X509Store(StoreLocation)

지정된 X509Store 값을 사용하여 StoreLocation 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using the specified StoreLocation value.

X509Store(StoreName)

지정된 X509Store 값을 사용하여 StoreName 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using the specified StoreName value.

X509Store(StoreName, StoreLocation)

지정된 X509StoreStoreName 값을 사용하여 StoreLocation 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using the specified StoreName and StoreLocation values.

X509Store(StoreName, StoreLocation, OpenFlags)
X509Store(String)

지정된 저장소 이름을 사용하여 X509Store 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using the specified store name.

X509Store(String, StoreLocation)

X509Store 열거형의 값과 StoreName 열거형의 값을 나타내는 문자열을 사용하여 StoreLocation 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the X509Store class using a string that represents a value from the StoreName enumeration and a value from the StoreLocation enumeration.

X509Store(String, StoreLocation, OpenFlags)

속성

Certificates

X.509 인증서 저장소에 있는 인증서 컬렉션을 반환합니다.Returns a collection of certificates located in an X.509 certificate store.

IsOpen
Location

X.509 인증서 저장소의 위치를 가져옵니다.Gets the location of the X.509 certificate store.

Name

X.509 인증서 저장소 이름을 가져옵니다.Gets the name of the X.509 certificate store.

StoreHandle

IntPtr 저장소에 대한 HCERTSTORE 핸들을 가져옵니다.Gets an IntPtr handle to an HCERTSTORE store.

메서드

Add(X509Certificate2)

X.509 인증서 저장소에 인증서를 추가합니다.Adds a certificate to an X.509 certificate store.

AddRange(X509Certificate2Collection)

X.509 인증서 저장소에 인증서 컬렉션을 추가합니다.Adds a collection of certificates to an X.509 certificate store.

Close()

X.509 인증서 저장소를 닫습니다.Closes an X.509 certificate store.

Dispose()

X509Store에서 사용하는 리소스를 해제합니다.Releases the resources used by this X509Store.

Equals(Object)

지정한 개체와 현재 개체가 같은지 여부를 확인합니다.Determines whether the specified object is equal to the current object.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 작동합니다.Serves as the default hash function.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type을 가져옵니다.Gets the Type of the current instance.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object.

(다음에서 상속됨 Object)
Open(OpenFlags)

OpenFlags 플래그 설정에 따라 X.509 인증서 저장소를 열거나 새 저장소를 만듭니다.Opens an X.509 certificate store or creates a new store, depending on OpenFlags flag settings.

Remove(X509Certificate2)

X.509 인증서 저장소에서 인증서를 제거합니다.Removes a certificate from an X.509 certificate store.

RemoveRange(X509Certificate2Collection)

X.509 인증서 저장소에서 인증서 범위를 제거합니다.Removes a range of certificates from an X.509 certificate store.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.Returns a string that represents the current object.

(다음에서 상속됨 Object)

적용 대상