開啟 Active Directory 存放區和擷取憑證
[CAPICOM 是 32 位的僅限元件,可用於下列作業系統:Windows Server 2008、Windows Vista 和 Windows XP。 請改用.NET Framework來實作安全性功能。 如需詳細資訊,請參閱 使用 CAPICOM 的替代方案。]
您可以從儲存 網域之使用者的憑證的 Active Directory 存放區擷取憑證。 Active Directory 存放區只能以唯讀模式開啟,而應用程式無法使用 CAPICOM 將憑證新增至 Active Directory 存放區或從 Active Directory 存放區中移除憑證。
在任何 CAPICOM 錯誤上,會傳回負數十進位值為 Err.Number 。 如需詳細資訊,請參閱 CAPICOM_ERROR_CODE。 如需 Err.Number的正十進位值相關資訊,請參閱 Winerror.h。
下列範例顯示開啟 Active Directory 存放區,並從該存放區擷取憑證。
Sub OpenADStore()
On Error GoTo ErrorHandler
Dim mystore As Store
Set mystore = New Store
' Put a string that represents the name of a certificate
' subject in SubjectNameCn. In the following example,
' the * wildcard character is used in the string so that
' the Active Directory store will be searched for all
' certificates with a subject name beginning with 'S.'
Dim SubjectNameCn As String
' The following uses 'cn=' and the * wildcard character.
' Using this string, all certificates in the Active Directory
' store with a subject name beginning with an 'S' would
' be returned.
SubjectNameCn = "CN=S*"
' Active Directory stores can only be opened with read-only
' access.
mystore.Open CAPICOM_ACTIVE_DIRECTORY_USER_STORE, _
SubjectNameCn, CAPICOM_STORE_OPEN_READ_ONLY
If mystore.Certificates.Count < 1 Then
MsgBox "A certificate for " & SubjectNameCn & _
" was not found "
Else
MsgBox "The certificate has been retrieved."
End If
Set mystore = 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