禁用证书验证

时间:2013-04-05 17:13:12

标签: web-services delphi delphi-xe2 x509certificate

我需要使用WCF,但它有证书,我需要禁用其身份验证。 有谁知道我怎么能在Delphi XE2中做到这一点?

我已经尝试过以下代码:

首次尝试:

Rio.HTTPWebNode.InvokeOptions:= [soIgnoreInvalidCerts,soAutoCheckAccessPointViaUDDI];

里约热内卢是THTTPRIO。

第二次尝试:

    class procedure ClasseTeste.OnBeforePost(const HTTPReqResp: THTTPReqResp;
      Data: Pointer);
    var
      SecurityFlags: DWord;
      SecurityFlagsLen: DWord;
      Request: HINTERNET;
      begin
      Request := Data;
      if soIgnoreInvalidCerts in Rio.HTTPWebNode.InvokeOptions then
      begin
        SecurityFlagsLen := SizeOf(SecurityFlags);
        InternetQueryOption(Request, INTERNET_OPTION_SECURITY_FLAGS,
        Pointer(@SecurityFlags), SecurityFlagsLen);
        SecurityFlags := SecurityFlags or INTERNET_FLAG_IGNORE_CERT_CN_INVALID;
        InternetSetOption(Request, INTERNET_OPTION_SECURITY_FLAGS,
        Pointer(@SecurityFlags), SecurityFlagsLen);
      end;
    end;

rio.HTTPWebNode.OnBeforePost:= ClasseTeste.OnBeforePost;

我可以使用c#执行此操作,代码如下:

channel = new ChannelFactory<WsMain.IWsInterface>("****");
                channel.Credentials.UserName.UserName = "*****";
                channel.Credentials.UserName.Password = "*****";
                channel.Credentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

1 个答案:

答案 0 :(得分:1)

在您的请求后添加以下行:

  SecurityFlags := SecurityFlags or SECURITY_FLAG_IGNORE_REVOCATION;
  InternetSetOption(Request, INTERNET_OPTION_SECURITY_FLAGS, Pointer(@SecurityFlags), SecurityFlagsLen);

会做什么:

procedure TForm1.RIO_OnBeforePost(const HTTPReqResp: THTTPReqResp; Data: Pointer);
  var
   SecurityFlags: DWord;
   SecurityFlagsLen: DWord;
   Request: HINTERNET;

begin
  Request := Data;

  SecurityFlagsLen := SizeOf(SecurityFlags);
  InternetQueryOption(Request, INTERNET_OPTION_SECURITY_FLAGS, Pointer(@SecurityFlags), SecurityFlagsLen);

  SecurityFlags := SecurityFlags or SECURITY_FLAG_IGNORE_UNKNOWN_CA;
  InternetSetOption(Request, INTERNET_OPTION_SECURITY_FLAGS, Pointer(@SecurityFlags), SecurityFlagsLen);

  // It solved my problem
  SecurityFlags := SecurityFlags or SECURITY_FLAG_IGNORE_REVOCATION;
  InternetSetOption(Request, INTERNET_OPTION_SECURITY_FLAGS, Pointer(@SecurityFlags), SecurityFlagsLen);


end;