Freigeben über


Sammeln und Überprüfen von Zertifikaten

[CAPICOM ist eine 32-Bit-Komponente, die in den folgenden Betriebssystemen verfügbar ist: Windows Server 2008, Windows Vista und Windows XP. Verwenden Sie stattdessen die .NET Framework, um Sicherheitsfeatures zu implementieren. Weitere Informationen finden Sie unter Alternativen zur Verwendung von CAPICOM.]

Häufig muss eine Gruppe von Zertifikaten gesammelt und überprüft werden. Dies geschieht häufig, um eine Gruppe von Empfängern auf eine umhüllte Nachricht vorzubereiten. Im folgenden Beispiel werden die Zertifikate in einem lokalen Speicher aufgezählt und auf Gültigkeit überprüft. Als Nächstes wird ein Active Directory-Speicher geöffnet, um neue Zertifikate abzurufen und dem lokalen Speicher hinzuzufügen. Die aus dem Active Directory-Speicher abgerufenen Zertifikate werden auf Gültigkeit überprüft und, falls gültig, dem lokalen Speicher hinzugefügt. Beide Filialen werden dann geschlossen.

Bei jedem CAPICOM-Fehler wird ein negativer Dezimalwert von Err.Number zurückgegeben. Weitere Informationen finden Sie unter CAPICOM_ERROR_CODE. Informationen zu positiven Dezimalwerten von Err.Number finden Sie unter Winerror.h.

In diesem Beispiel wird der Name des lokalen Speichers als Zeichenfolgenparameter übergeben. Eine Zeichenfolge, die die Suchkriterien für Zertifikate im Active Directory-Speicher angibt, wird ebenfalls als Parameter übergeben.

Sub CollectValidCerts(ByVal storename As String, ByVal _
    certname As String)

    On Error GoTo errorhandler

    ' Prepare a local certificate store to contain valid
    ' certificates for the recipients of an enveloped
    ' message.

    ' Open the local store and go to the certificates in the store
    '   1. Display the certificate
    '   2. Check the validity of the certificate
    '   3. Remove certificates that are not valid from the store

    Dim LocalStore As New Store
    Dim ADStore As New Store
    Dim i As Long

    LocalStore.Open(CAPICOM_CURRENT_USER_STORE, storename, _
        CAPICOM_STORE_OPEN_READ_WRITE)

    MsgBox("There are " & LocalStore.Certificates.Count & _
        " certificates in this store ")
    For i = 1 To LocalStore.Certificates.Count
        If LocalStore.Certificates.Item(i).IsValid Then
            LocalStore.Certificates.Item(i).Display()
        Else
            MsgBox("A certificate that is not valid was found.")
        End If
    Next i

    ' Open the AD store and retrieve a certificate based
    ' on a string passed into the function. Add any valid
    ' certificates found to the local store.

    ADStore.Open(CAPICOM_ACTIVE_DIRECTORY_USER_STORE, certname, _
        CAPICOM_STORE_OPEN_READ_ONLY)
    MsgBox("There are " & ADStore.Certificates.Count & _
        " certificates in the AD store.")

    For i = 1 To ADStore.Certificates.Count
        If ADStore.Certificates.Item(i).IsValid Then
            ADStore.Certificates.Item(i).Display()
            LocalStore.Add(ADStore.Certificates.Item(i))
        Else
            MsgBox("the certificate from the AD store is not valid.")
        End If
    Next i

    LocalStore = Nothing
    ADStore = Nothing
    MsgBox("Sub finished without error ")
    Exit Sub
errorhandler:
    If Err.Number > 0 Then
        MsgBox("Visual Basic error found:" & Err.Description)
    Else
        MsgBox("CAPICOM error found : " & Err.Number)
    End If
End Sub