共用方式為


收集和驗證憑證

[CAPICOM 是 32 位的僅限元件,可用於下列作業系統:Windows Server 2008、Windows Vista 和 Windows XP。 請改用 .NET Framework 來實作安全性功能。 如需詳細資訊,請參閱 使用 CAPICOM 的替代方案。]

通常必須收集並驗證一組 憑證 。 這通常是為了準備信封郵件的收件者群組。 在下列範例中,會列舉並檢查本機存放區中的憑證是否有效。 接下來,會開啟 Active Directory 存放區,以擷取並新增至本機存放區的新憑證。 從 Active Directory 存放區擷取的憑證會檢查其有效性,如果有效,則會新增至本機存放區。 然後,這兩個商店都會關閉。

在任何 CAPICOM 錯誤上,會傳回 Err.Number 的負十進位值。 如需詳細資訊,請參閱 CAPICOM_ERROR_CODE。 如需 Err.Number的正十進位值相關資訊,請參閱 Winerror.h。

在此範例中,本機存放區的名稱會以字串參數的形式傳入。 字串,表示 Active Directory 存放區中憑證的搜尋準則也會當做參數傳入。

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