HttpWebRequest and client certificates

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

    HttpWebRequest and client certificates

    Hi all,
    I'm having trouble connecting to a Java web service using HttpWebRequest. I
    get the error message "The request was aborted: Could not create SSL/TLS
    secure channel". The Java service requires a client certificate which they
    have provided in .cer format. I can connect ok to their test service which
    uses SSL but doesnt require a client certificate with the code below.

    I dont have great deal of knowledge about client certificates so anyone got
    any ideas why this isnt working?

    Thanks
    Steve

    Details are:
    Tried using .net 1.1 and .net 2.0.
    Running this code from a Winform application.

    I'm connecting using the code below

    System.Net.Serv icePointManager .CertificatePol icy = new
    TrustAllCertifi catePolicy();
    string url="https://www.websiteurlh ere.com";
    XmlDocument doc = new XmlDocument();
    doc.Load(xml file containing request);

    string data=doc.InnerX ml;
    byte[] buffer=Encoding .UTF8.GetBytes( data);
    HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(url);
    req.Method = "POST";
    req.ProtocolVer sion = HttpVersion.Ver sion10;
    req.KeepAlive = false;
    req.Headers.Cle ar();

    X509Certificate cert =
    X509Certificate .CreateFromCert File(@"C:\Devel op\cert_sign.ce r");
    req.ClientCerti ficates.Add(cer t);
    req.ContentLeng th = buffer.Length;
    req.ContentType ="applicatio n/x-www-form-urlencoded";

    using (Stream reqst = req.GetRequestS tream())
    {
    reqst.Write(buf fer, 0, buffer.Length);
    reqst.Flush();
    }
    using (HttpWebRespons e response = (HttpWebRespons e)req.GetRespon se())
    {
    using (Stream rs = response.GetRes ponseStream())
    {
    using (StreamReader sr = new StreamReader(rs ))
    {
    string responseDetails = sr.ReadToEnd();
    textBox1.Text = responseDetails ;
    }
    }
    }

  • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

    #2
    RE: HttpWebRequest and client certificates

    http://support.microsoft.com/kb/895971 sounds like it describes what you want
    to do.

    --
    Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

    Microsoft MVP, Visual Developer - Visual C#


    "steveS" wrote:
    Hi all,
    I'm having trouble connecting to a Java web service using HttpWebRequest. I
    get the error message "The request was aborted: Could not create SSL/TLS
    secure channel". The Java service requires a client certificate which they
    have provided in .cer format. I can connect ok to their test service which
    uses SSL but doesnt require a client certificate with the code below.
    >
    I dont have great deal of knowledge about client certificates so anyone got
    any ideas why this isnt working?
    >
    Thanks
    Steve
    >
    Details are:
    Tried using .net 1.1 and .net 2.0.
    Running this code from a Winform application.
    >
    I'm connecting using the code below
    >
    System.Net.Serv icePointManager .CertificatePol icy = new
    TrustAllCertifi catePolicy();
    string url="https://www.websiteurlh ere.com";
    XmlDocument doc = new XmlDocument();
    doc.Load(xml file containing request);
    >
    string data=doc.InnerX ml;
    byte[] buffer=Encoding .UTF8.GetBytes( data);
    HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(url);
    req.Method = "POST";
    req.ProtocolVer sion = HttpVersion.Ver sion10;
    req.KeepAlive = false;
    req.Headers.Cle ar();
    >
    X509Certificate cert =
    X509Certificate .CreateFromCert File(@"C:\Devel op\cert_sign.ce r");
    req.ClientCerti ficates.Add(cer t);
    req.ContentLeng th = buffer.Length;
    req.ContentType ="applicatio n/x-www-form-urlencoded";
    >
    using (Stream reqst = req.GetRequestS tream())
    {
    reqst.Write(buf fer, 0, buffer.Length);
    reqst.Flush();
    }
    using (HttpWebRespons e response = (HttpWebRespons e)req.GetRespon se())
    {
    using (Stream rs = response.GetRes ponseStream())
    {
    using (StreamReader sr = new StreamReader(rs ))
    {
    string responseDetails = sr.ReadToEnd();
    textBox1.Text = responseDetails ;
    }
    }
    }
    >
    >

    Comment

    • steveS

      #3
      Re: HttpWebRequest and client certificates

      Hi Peter,
      Thanks for the reply and the link but I've already read that article.
      I'm struggling to find information on using client certificates for example
      when I double click on the client certificate the general tab displays a
      message "Windows does not have enough information to verify this
      certificate", but I can continue to install the certificate ok. Does this
      message mean that the certificate is invalid in someway?

      Steve


      "Peter Ritchie [C# MVP]" <PRSoCo@newsgro ups.nospamwrote in message
      news:3367EAB8-9586-4F28-8F77-D6C19322320E@mi crosoft.com...
      http://support.microsoft.com/kb/895971 sounds like it describes what you
      want
      to do.
      >
      --
      Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

      Microsoft MVP, Visual Developer - Visual C#
      >
      >
      "steveS" wrote:
      >
      >Hi all,
      >I'm having trouble connecting to a Java web service using HttpWebRequest.
      >I
      >get the error message "The request was aborted: Could not create SSL/TLS
      >secure channel". The Java service requires a client certificate which
      >they
      >have provided in .cer format. I can connect ok to their test service
      >which
      >uses SSL but doesnt require a client certificate with the code below.
      >>
      >I dont have great deal of knowledge about client certificates so anyone
      >got
      >any ideas why this isnt working?
      >>
      >Thanks
      >Steve
      >>
      >Details are:
      >Tried using .net 1.1 and .net 2.0.
      >Running this code from a Winform application.
      >>
      >I'm connecting using the code below
      >>
      > System.Net.Serv icePointManager .CertificatePol icy = new
      >TrustAllCertif icatePolicy();
      > string url="https://www.websiteurlh ere.com";
      > XmlDocument doc = new XmlDocument();
      > doc.Load(xml file containing request);
      >>
      > string data=doc.InnerX ml;
      > byte[] buffer=Encoding .UTF8.GetBytes( data);
      > HttpWebRequest req = (HttpWebRequest )WebRequest.Cre ate(url);
      > req.Method = "POST";
      > req.ProtocolVer sion = HttpVersion.Ver sion10;
      > req.KeepAlive = false;
      > req.Headers.Cle ar();
      >>
      > X509Certificate cert =
      >X509Certificat e.CreateFromCer tFile(@"C:\Deve lop\cert_sign.c er");
      > req.ClientCerti ficates.Add(cer t);
      > req.ContentLeng th = buffer.Length;
      > req.ContentType ="applicatio n/x-www-form-urlencoded";
      >>
      > using (Stream reqst = req.GetRequestS tream())
      > {
      > reqst.Write(buf fer, 0, buffer.Length);
      > reqst.Flush();
      > }
      > using (HttpWebRespons e response = (HttpWebRespons e)req.GetRespon se())
      > {
      > using (Stream rs = response.GetRes ponseStream())
      > {
      > using (StreamReader sr = new StreamReader(rs ))
      > {
      > string responseDetails = sr.ReadToEnd();
      > textBox1.Text = responseDetails ;
      > }
      > }
      > }
      >>
      >>

      Comment

      Working...