1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, CheckSedexWebService1,
Soap.InvokeRegistry, Soap.Rio, Soap.SOAPHTTPClient,
comobj, WinInet, capicom_tlb, Soap.SOAPHTTPTrans;
class procedure TForm1.setCertificate(request: HINTERNET);
var
i: integer;
store: TStore;
c: ICertificate2;
cert: TCertificate;
certs: TCertificates;
ov: OleVariant;
CertContext: ICertContext;
PCertContext: PCCERT_CONTEXT;
begin
store := TStore.create(nil);
store.Open(CAPICOM_CURRENT_USER_STORE, 'MY', CAPICOM_STORE_OPEN_READ_ONLY);
certs := TCertificates.create(nil);
certs.ConnectTo(store.Certificates as ICertificates2);
cert := TCertificate.create(nil);
for i := 1 to certs.Count do
begin
ov := (certs.Item[i]);
c := IDispatch(ov) as ICertificate2;
cert.ConnectTo(IDispatch(ov) as ICertificate2);
if cert.HasPrivateKey And (cert.ValidFromDate <= Now) And (cert.ValidToDate >= Now) then
begin
CertContext := c as ICertContext;
CertContext.Get_CertContext(integer(PCertContext));
if not(InternetSetOption(request, INTERNET_OPTION_CLIENT_CERT_CONTEXT, PCertContext, Sizeof(CertContext) * 5)) then raise Exception.create('Erreur lors de l''attribution du certificat: ' + inttostr(GetLastError));
Break;
end;
end;
store.Close;
certs.Free;
store.Free;
end; |
Partager