Share via


ICertAdmin::ImportCertificate 方法 (certadm.h)

ImportCertificate 方法會採用先前發行的憑證,並將它匯入證書頒發機構單位的 (CA) 資料庫。 此方法最初是在 ICertAdmin 介面中定義。

如需憑證必須符合才能成功匯入的需求,請參閱。

語法

HRESULT ImportCertificate(
  [in]  const BSTR strConfig,
  [in]  const BSTR strCertificate,
  [in]  LONG       Flags,
  [out] LONG       *pRequestId
);

參數

[in] strConfig

代表證書頒發機構單位的有效組態字串,格式為 COMPUTERNAME\CANAME,其中 COMPUTERNAME 是憑證服務伺服器的網路名稱,而 CANAME 是證書頒發機構單位的一般名稱,如憑證服務設定期間輸入。 如需組態字串名稱的相關信息,請參閱 ICertConfig

重要ImportCertificate 不會在設定字串變更時清除內部快取。 當您變更 CA 的組態字串時,您必須具現化新的 ICertAdmin 物件,並使用新的組態字串再次呼叫此方法。
 

[in] strCertificate

要匯入之憑證的二進位表示法。

[in] Flags

指定憑證的格式。 此參數可以是下列其中一個值。

意義
CR_IN_BASE64HEADER
BASE64 格式開頭/結尾。
CR_IN_BASE64
不含開始/結束的BASE64格式。
CR_IN_BINARY
二進位格式。

[out] pRequestId

LONG 值的指標,這個值會接收匯入憑證的資料庫指派要求標識符。

傳回值

C++

如果方法成功,且 pRequestID 參數設定為匯入憑證的資料庫指派要求標識符值,則方法會傳回S_OK。

如果方法失敗,它會傳回 HRESULT 值,指出錯誤。 如需常見錯誤碼的清單,請參閱 一般 HRESULT 值

VB

傳回值是匯入憑證的資料庫指派要求標識符。

備註

ImportCertificate 方法適用於已部分從備份還原的證書頒發機構單位:如果憑證不在用來還原證書頒發機構單位的備份磁帶上,但存在於檔案中,則可以透過此方法匯入憑證。

若要讓這個方法成功,必須先前由 strConfig 中指定的證書頒發機構單位核發要匯入的憑證。 還原的證書頒發機構單位會驗證憑證的簽章,如果簽章無效,方法呼叫將會失敗。

此外,如果憑證已存在於資料庫中,您就無法匯入憑證。 資料庫中的每個憑證都必須是唯一的。 資料庫會檢查憑證的序號,以確保唯一性。

範例

// This code imports a binary certificate file.
BSTR   bstrCert = NULL;  // Variable for certificate.
HANDLE hFile;  
DWORD  cchFile, cbRead;
LONG   nID;  // Variable for request ID.

// Open the file that contains the certificate.
hFile = CreateFile((LPCSTR) "d:\\cert1.cer",
                  GENERIC_READ,
                  FILE_SHARE_READ,
                  NULL,
                  OPEN_EXISTING,
                  0,
                  NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
    printf("Unable to open file\n");
    // Take error action as needed.
}
// Determine the file size.
cchFile = GetFileSize(hFile, NULL);
if ( (DWORD)-1 == cchFile )
{
    printf("Failed GetFileSize\n");
    CloseHandle(hFile);
    // Take error action as needed.
}
// Allocate the memory for the certificate.
bstrCert = SysAllocStringByteLen(NULL, cchFile);
if (NULL == bstrCert)
{
    printf("Failed SysAllocStringByteLen\n");
    CloseHandle(hFile);
    // Take error action as needed.
}
// Read in the certificate.
if (!ReadFile(hFile,
             (char *)bstrCert,
             cchFile,
             &cbRead,
             NULL) || (cbRead != cchFile))
{
    printf("Failed to successfully read file\n");
    CloseHandle(hFile);
    SysFreeString(bstrCert);
    // Take error action as needed.
}
// Close the file.
CloseHandle(hFile);

// Import the certificate.
bstrCA = SysAllocString(L"<COMPUTERNAMEHERE>\\<CANAMEHERE>");
if (FAILED(hr))
{
    printf("Failed to allocate memory for bstrCA\n");
    SysFreeString(bstrCert);
    // Take error action as needed.
}

hr = pCertAdmin->ImportCertificate(bstrCA,
                                   bstrCert,
                                   CR_IN_BINARY,
                                   &nID);
if (FAILED(hr))
    printf("Failed ImportCertificate [%x]\n", hr);
else
    printf("Imported certificated has Request ID: %d\n", nID);

SysFreeString(bstrCert);
SysFreeString(bstrCA);

規格需求

需求
最低支援的用戶端 都不支援
最低支援的伺服器 Windows Server 2003 [僅限傳統型應用程式]
目標平台 Windows
標頭 certadm.h (包含 Certsrv.h)
程式庫 Certidl.lib
Dll Certadm.dll

另請參閱

CCertAdmin

ICertAdmin

ICertAdmin2

ICertConfig