exception using HTTPWebRequest with SSL



VS2005 VB.net

I'm using the HTTPWebRequest class to connect to a web site with SSL. I
first manually connected to the site and installed the certificate on my
computer, and then use the X509Certificate class to add the certificate to
the HTTPWebRequest, but I continue to get the exception:

"The underlying connection was closed: Could not establish trust
relationship for the SSL/TLS secure channel."

Inner Exception:
"The remote certificate is invalid according to the validation
procedure."

KB895971 says the certificate should be installed in the LOCAL_MACHINE
registry hive, but it got installed in the CURRENT_USER registry hive.

Is this the problem?

How do I install the certificate in the LOCAL_MACHINE hive instead of the
CURRENT_USER hive?

Thanks
Bill
----------------------------------------------------------------------------------------------
I have tested the code below on a non-SSL site and it works fine.
Sub SendRequest()

Dim sHTTPResponse As String = ""

Dim url As String = "https://www.abc.com/login.asp";

Dim myHttpWebRequest As HttpWebRequest

Dim myHttpWebResponse As HttpWebResponse

myHttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

myHttpWebRequest.Method = "POST"

Dim postData As String = "username" + ChrW(61) + "uname"

postData += "&password" + ChrW(61) + "pwd"

Dim encoding As New ASCIIEncoding()

Dim byte1 As Byte() = encoding.GetBytes(postData)

' Set the content type of the data being posted.

myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"

' Set the content length of the string being posted.

myHttpWebRequest.ContentLength = byte1.Length

Dim cert As X509Certificate =
X509Certificate.CreateFromCertFile("C:\Documents and Settings\xxx\My
Documents\Certificate.cer")

myHttpWebRequest.ClientCertificates.Add(cert)

Dim newStream As Stream = myHttpWebRequest.GetRequestStream()

newStream.Write(byte1, 0, byte1.Length)



' Sends the request and waits for a response.

myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

Dim sr As New StreamReader(myHttpWebResponse.GetResponseStream)

Dim res As String = sr.ReadToEnd

Me.TextBox1.Text = res

End Sub


.



Relevant Pages

  • Re: using p12 Certificate - converting to x509 What am I doing wrong?
    ... their staff sent it to me in .pfx format for me to import password ... Dim h as httpwebrequest ... So i wrote a small aspx page that checked for the certificate on ...
    (microsoft.public.dotnet.security)
  • Re: using p12 Certificate - converting to x509 What am I doing wrong?
    ... but .net currently only supports binary DER ... > Dim h as httpwebrequest ... > h = Ctype, httpwebrequest) ... So i wrote a small aspx page that checked for the certificate on ...
    (microsoft.public.dotnet.security)
  • Re: HTTPS Posting Help needed
    ... than the X509Certificate.CreateFromSignedFile method to read the certificate ... from your .cer file? ... > Dim data As Byte= encoding.GetBytes ... > HttpWebRequest) ...
    (microsoft.public.dotnet.security)
  • x509 cert not being submitted
    ... I am trying to attach a certificate to a httpwebrequest and send it. ... Dim h as httpwebrequest ...
    (microsoft.public.dotnet.security)
  • Re: socket error
    ... It ended up with something to do with our Proxy configuration. ... but on the 2.0 box for some reason the httpwebrequest couldnt ... Yes I can visit that page directly through a browser on the server. ... >> | Dim returnstr As String ...
    (microsoft.public.dotnet.framework.aspnet)