IHttpRequest::GetClientCertificate 方法

检索与请求关联的客户端证书。

语法

HRESULT GetClientCertificate(  
   OUT HTTP_SSL_CLIENT_CERT_INFO** ppClientCertInfo,  
   OUT BOOL* pfClientCertNegotiated  
);  

parameters

ppClientCertInfo
[OUT]指向 HTTP_SSL_CLIENT_CERT_INFO 结构的指针。

pfClientCertNegotiated
[OUT] true 如果客户端证书已协商,则为 ;否则为 false。 有关详细信息,请参见“备注”部分。

返回值

HRESULT。 可能的值包括(但并不限于)下表中的项。

定义
S_OK 指示未发生错误,但不保证找到证书。 有关详细信息,请参见“备注”部分。
HRESULT_FROM_WIN32 (ERROR_NOT_FOUND) 指示找不到客户端证书。 ERROR_NOT_FOUND在 Winerror.h 中定义。
ERROR_INVALID_PARAMETER 指示 ppClientCertInfopfClientCertNegotiated 参数为 NULL。

备注

成功的 HRESULT 不保证找到客户端证书。 开发人员还必须验证 ppClientCertInfo 是否不为 NULL。

pfClientCertNegotiatedtrue不保证 ppClientCertInfo 不为 NULL。

开发人员可以使用 GetClientCertificate 方法检索与当前请求关联的客户端证书。 调用 GetClientCertificate 方法后, ppClientCertInfo 参数将包含指向 HTTP_SSL_CLIENT_CERT_INFO 结构的指针,如果客户端证书可用,该结构将包含客户端证书;如果没有可用的证书,则为 NULL。

对于不需要客户端证书的 URL,可以在调用 以尝试手动加载客户端证书之前调用 GetClientCertificateNegotiateClientCertificate 方法。

示例

以下示例演示如何通过实现 CHttpModule::OnBeginRequest 方法获取指向 HTTP_SSL_CLIENT_CERT_INFO 结构的指针。

void   checkForClientCert(IHttpContext*  pHttpContext)
{
   static long cnt;     
   // keep track of how many times we are called
   InterlockedIncrement (&cnt);  
   HRESULT hr = S_OK;
   IHttpRequest *pIHTTPR = pHttpContext->GetRequest();
   HTTP_REQUEST * pRawRequest = pIHTTPR->GetRawHttpRequest();
   HTTP_COOKED_URL hcu = pRawRequest->CookedUrl;

   // Send URL and count to the trace window
   TRC_MSGW_FULL(L"cnt = " << cnt << " URI: " << hcu.pFullUrl);

   // return immediately if not a HTTPS request
   if ( pRawRequest->pSslInfo == NULL ){
      TRC_MSG( "connection is not using SSL");
      return;
   }

   HTTP_SSL_CLIENT_CERT_INFO *pClientCertInfo=NULL;
   BOOL fccNeg;
   hr = pIHTTPR->GetClientCertificate(&pClientCertInfo,&fccNeg);

   // If you have not selected "Require Client Certificates" or called
   // NegotiateClientCertificate(), you  may get  ERROR_NOT_FOUND 

   if( hr == HRESULT_FROM_WIN32(ERROR_NOT_FOUND) ||
      pClientCertInfo == NULL ){
         TRC_HR_MSG(hr, "Cert not found" );
         return;
   }
   if(FAILED(hr)){
      LOG_ERR_HR("GetClientCertificate", hr);
      return;
   }	

   // You must verify pClientCertInfo != NULL

   if( fccNeg && pClientCertInfo != NULL){
      ULONG uSiz = pClientCertInfo->CertEncodedSize;
      TRC_MSG( "cert size: " << uSiz \
         << " Previously negotiated " << fccNeg );
      // compute the CRC check sum of the certificate
      unsigned long certCrc = genCRC(pClientCertInfo->pCertEncoded,
         pClientCertInfo->CertEncodedSize);
      TRC_MSG( "cert crc: " << certCrc );
   }
   else
      TRC_MSG( "No client certificate. fccNeg = " << fccNeg );
}

REQUEST_NOTIFICATION_STATUS
CMyHttpModule::OnBeginRequest(
                              IHttpContext*       pHttpContext,
                              IHttpEventProvider* // pProvider  
                              )
{
   checkForClientCert(pHttpContext);
   return RQ_NOTIFICATION_CONTINUE;
}

有关如何创建和部署本机 DLL 模块的详细信息,请参阅 演练:使用本机代码创建 Request-Level HTTP 模块

可以选择使用调用约定编译代码, __stdcall (/Gz) 而不是为每个函数显式声明调用约定。

要求

类型 说明
客户端 - Windows Vista 上的 IIS 7.0
- Windows 7 上的 IIS 7.5
- Windows 8 上的 IIS 8.0
- Windows 10 上的 IIS 10.0
服务器 - Windows Server 2008 上的 IIS 7.0
- Windows Server 2008 R2 上的 IIS 7.5
- Windows Server 2012 上的 IIS 8.0
- Windows Server 2012 R2 上的 IIS 8.5
- Windows Server 2016 上的 IIS 10.0
产品 - IIS 7.0、IIS 7.5、IIS 8.0、IIS 8.5、IIS 10.0
- IIS Express 7.5、IIS Express 8.0、IIS Express 10.0
Header Httpserv.h

另请参阅

IHttpRequest 接口
IHttpRequest::NegotiateClientCertificate 方法