Article

From:
To:
Jan Fabinger
Subject:
Re: Using THTTPRIO with client side certificate
Newsgroup:
embarcadero.public.delphi.webservices

Re: Using THTTPRIO with client side certificate

> {quote:title=Jan Fabinger wrote:}{quote}
> Hi,
> 
> I'm connecting with THTTPRIO to a server and using WebServices that i generate with WSDLImp.exe.
> There are a two types of authorization. Basic authorization with user name and password and the second with client side certificate and user name, password.
> 
> For basic authorization I'm using: 
> 
> Rio.HTTPWebNode.OnBeforePost := EvHandler.OnBeforePost;
> ...
> procedure TEventHandlers.OnBeforePost(const HTTPReqResp: THTTPReqResp; Data:Pointer);
> begin
>    if not InternetSetOption(Data, INTERNET_OPTION_USERNAME,
>           PChar(UserName),
>           Length(UserName))then
>           ShowMessage(SysErrorMessage(GetLastError));
>    if not InternetSetOption(Data, INTERNET_OPTION_PASSWORD,
>            PChar(Password),
>            Length (Password)) then
>            ShowMessage(SysErrorMessage(GetLastError));
> end;
> 
> All works fine.
> 
> If i want to authorizate with certificate, I add to OnBeforePost:
> 
> var
>  PCertContext:PCCERT_CONTEXT;
> ...
>    if not InternetSetOption( Data, INTERNET_OPTION_CLIENT_CERT_CONTEXT,
>           PCertContext, Sizeof( CERT_CONTEXT ) ) then
>           ShowMessage(SysErrorMessage(GetLastError));
> 
> Now, if I call a webservice, pop up a window that want username and pasword. If I insert it and click ok, window pop up again and again...
> Clicking on Storno throw exeption 401: Unauthorized.
> What I'm doing bad?
> 
> Thanks for help, 
>  Jan.

Hi Jan,

Are you loading the certificate before call the InternetSetOption function? Like this (using the CAPICOM.DLL) procedure THttpRIO.OnBeforePost(const HTTPReqResp: THTTPReqResp; Data:Pointer); var   Cert : ICertificate2;   CertContext : ICertContext;   PCertContext : PCCERT_CONTEXT; begin      //Loading a certificate from a .P12 file.      Cert:=CreateComObject(CLASS_Certificate) as ICertificate2;
Cert.Load(cFileName_p12,cPassword,CAPICOM_KEY_STORAGE_DEFAULT,CAPICOM_CURRENT_USER_KEY);
     CertContext := Cert as ICertContext;
     CertContext.Get_CertContext( Integer( PCertContext ) );
     // Pone el certificado para la comunicaci__n SSL
     if InternetSetOption( Data, INTERNET_OPTION_CLIENT_CERT_CONTEXT,
                    PCertContext, Sizeof( CERT_CONTEXT ) ) = False then
               ShowMessage( 'No se ha podido adjuntar el certificado digital');
end;

Obviously, certificates can be read from smart card: Store.Open(CAPICOM_SMART_CARD_USER_STORE, 'MY',CAPICOM_STORE_OPEN_READ_ONLY); or from the standard store: Store.Open(CAPICOM_CURRENT_USER_STORE, "My", CAPICOM_STORE_OPEN_READ_ONLY);

I hope that this is for your help

Salva
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Thu, 28 Mar 2024 23:09:23 UTC
Copyright © 2009-2024
HREF Tools Corp.