共用方式為


將憑證新增至憑證Microsoft Store

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

如果以讀取/寫入權限開啟存放區,則可以將憑證新增至憑證存放區或從憑證存放區中移除。 讀取/寫入權限不會授與 Active Directory 存放區。 雖然可以在記憶體存放區中新增或移除憑證,但不會在會話之間保存記憶體存放區中的變更。

您可以使用 Add 方法,將憑證新增至以讀取/寫入權限開啟的憑證存放區。 您可以使用 Remove 方法,從以讀取/寫入權限開啟的憑證存放區中移除憑證。 新商店可以建立並儲存在CAPICOM_CURRENT_USER_STORE和CAPICOM_LOCAL_MACHINE_STORE位置。 新建立的存放區可以透過讀取/寫入權限開啟其中一個位置。

在下列範例中,會開啟兩個憑證存放區。 從 Active Directory 存放區擷取名稱開頭為 F 的主體憑證。 CAPICOM_CURRENT_USER_STORE,CAPICOM_CA_STORE存放區接著會開啟為讀取/寫入存放區,並將 Active Directory 存放區中憑證集合中的第一個憑證新增至CAPICOM_CA_STORE中的憑證。

為了示範目的,此範例會顯示CAPICOM_MEMORY_STORE、CAPICOM_CURRENT_USER_STORE和CAPICOM_LOCAL_MACHINE_STORE位置中的商店開啟。 此範例示範如何從開啟的存放區匯出所有憑證、將匯出的憑證寫入檔案、讀取它們,以及將它們匯入至不同的存放區。 新匯入的憑證會列舉並顯示。

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

下列範例示範在宣告Microsoft Store物件和建立這些物件的實例中使用早期系結來開啟憑證存放區。

Sub AddCert()
On Error GoTo ErrorHandler
' The following shows two different ways to declare and
' create a store object.

Dim myADstore As New Store

Dim myCAstore As Store
Set myCAstore = New Store

' In this example, the Active Directory store will be searched for a 
' certificate with a subject name that begins with the letter F. 
' This is done by using the string "SN=F*" as the name of the store.

Dim SubjectNameSN As String
SubjectNameSN = "SN=F*"

' Active Directory stores can only be opened with read-only
' access.

myADstore.Open CAPICOM_ACTIVE_DIRECTORY_USER_STORE,
                SubjectNameSN , CAPICOM_STORE_OPEN_READ_ONLY

'  This example assumes that the store opened and that
'  at least one certificate was returned.
'  A complete application would ensure that at least one certificate
'  was in the store before proceeding and would
'  also select one or more of the certificates returned
'  to be added instead of using the first certificate
'  in the collection.

'  Open the MY store so that a certificate can be added.

myCAstore.Open CAPICOM_CURRENT_USER_STORE, CAPICOM_MY_STORE,
                    CAPICOM_STORE_OPEN_READ_WRITE

myCAstore.Add myADstore.certificates.Item(1)

' Release the two store objects.

Set myCAstore = Nothing
Set myADstore = Nothing
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