Bagikan melalui


Mengumpulkan dan Memverifikasi Sertifikat

[CAPICOM adalah komponen 32-bit saja yang tersedia untuk digunakan dalam sistem operasi berikut: Windows Server 2008, Windows Vista, dan Windows XP. Sebagai gantinya, gunakan .NET Framework untuk menerapkan fitur keamanan. Untuk informasi selengkapnya, lihat Alternatif untuk Menggunakan CAPICOM.]

Seringkali sekelompok sertifikat perlu dikumpulkan dan diverifikasi. Ini akan sering dilakukan untuk menyiapkan sekelompok penerima untuk pesan yang diselimuti. Dalam contoh berikut, sertifikat di penyimpanan lokal dijumlahkan dan diperiksa validitasnya. Selanjutnya, penyimpanan Direktori Aktif dibuka untuk mengambil dan menambahkan ke sertifikat baru penyimpanan lokal. Sertifikat yang diambil dari penyimpanan direktori aktif diperiksa validitasnya dan, jika valid, ditambahkan ke penyimpanan lokal. Kedua toko kemudian ditutup.

Pada kesalahan CAPICOM apa pun, nilai desimal negatif Err.Number dikembalikan. Untuk informasi selengkapnya, lihat CAPICOM_ERROR_CODE. Untuk informasi tentang nilai desimal positif Err.Number, lihat Winerror.h.

Dalam contoh ini, nama penyimpanan lokal diteruskan sebagai parameter string. String yang menunjukkan kriteria pencarian untuk sertifikat di penyimpanan Direktori Aktif juga diteruskan sebagai parameter.

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