Help with HTTPWebRequest and client certificates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nomad

    Help with HTTPWebRequest and client certificates

    Hi,

    I am using HTTPWebRequest to add a client certificate. I am then
    using HTTPWebRequest to POST xml to a web service which requires the
    attached client certificate to authenticate with their server
    certificate. However, I keep getting an error "Unable to connect to
    remote server". I know I am able to connect to their server and
    retrieve an XML response as we were able to do this using our old
    solution (WinHTTP in Visual Basic 6). I have read many articles and
    some of them have mentioned ignoring all certificate errors which I am
    also doing but with no luck. If anyone has any suggestion it would be
    greatly appreciated. The code to add the certificate is below.

    webRequest.Clie ntCertificates. Add(GetCertific ate());

    private X509Certificate GetCertificate( )
    {
    ///Set store to LocalMachine as this is where the
    certificates must be installed
    X509Store store = new X509Store(Store Name.My,
    StoreLocation.L ocalMachine);
    store.Open(Open Flags.ReadOnly) ;
    //Find certificate based on it's name
    X509Certificate 2Collection certificates =
    store.Certifica tes.Find(X509Fi ndType.FindBySu bjectName, this.sslName,
    true);
    return certificates[0];


    }

    The code to ignore certificate errors is below:

    ServicePointMan ager.ServerCert ificateValidati onCallback =
    new
    RemoteCertifica teValidationCal lback(IgnoreCer tificateErrorHa ndler);

    private bool IgnoreCertifica teErrorHandler( object sender,
    X509Certificate certificate,
    X509Chain chain,
    SslPolicyErrors sslPolicyErrors )
    {
    //I would log a Information Error so we know if partner
    site is iffy on the security side
    //But for now, as simple skip error will do
    return true;
    }

    Sending the XML is below. It fails on using (Stream os =
    this.webRequest .GetRequestStre am())

    public string Send(string dataToSend)
    {
    //Convert our string to a byte array
    byte[] bytes = Encoding.ASCII. GetBytes(dataTo Send);
    this.webRequest .ContentLength = bytes.Length;

    //write bytes to server
    using (Stream os = this.webRequest .GetRequestStre am())
    {
    os.Write(bytes, 0, bytes.Length);
    }

    //Get the response
    WebResponse webResponse = webRequest.GetR esponse();

    if (webResponse != null)
    {
    using (webResponse)
    {
    using (StreamReader sr = new
    StreamReader(we bResponse.GetRe sponseStream(), Encoding.Defaul t))
    {
    //return the data as string
    return sr.ReadToEnd(). Trim();
    }
    }
    }
    else //we got no response, we return null
    {
    return null;
    }

    }


    Thanks

Working...