title: Two-way authentication using ssl in dotnet
body: <p>I have a project where I need to send a datafile through a web request.  We need to setup Two-way authentication also known as mutual authentication.  We are not sure if we need a special cert or not but we know that it needs to be level 3.</p>
  
  <p>I am having trouble finding sample code for this case. I don't know where to add our cert information.  With this code a <code>Underlying connection is closed</code> error is thrown when we try to read the response stream and <code>ServicePointManager.ServerCertificateValidationCallback</code> is never called.  Here is what I have:</p>
  
  <pre class="lang-vb prettyprint-override"><code>ServicePointManager.ServerCertificateValidationCallback = New Security.RemoteCertificateValidationCallback(AddressOf MyCertValidationCb)
              httpReq = CType(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
              For Each cert As String In certs
                  X509cert = X509Certificate2.CreateFromCertFile(cert)
                  X509cert2 = New X509Certificate2(X509cert)
                  httpReq.ClientCertificates.Add(X509cert2)
              Next
              httpReq.Method = "POST"        ' Post method
              httpReq.ContentType = "text/xml"               ' content type
  
              ' Wrap the request stream with a text-based writer
              writer = New StreamWriter(httpReq.GetRequestStream())
              ' Write the XML text into the stream
              reader = New StreamReader(filename.Name)
              ret = reader.ReadToEnd()
              reader.Close()
              ' Send the data to the webserver
              writer.WriteLine(ret)
              writer.Close()
              ' Wait for response
              Dim httpRsp As System.Net.HttpWebResponse = CType(httpReq.GetResponse(), HttpWebResponse)
              sr = New StreamReader(httpRsp.GetResponseStream)
              responseText = sr.ReadToEnd
  
              If httpReq IsNot Nothing Then
                  httpReq.GetRequestStream().Close()
              End If
              If httpRsp IsNot Nothing Then
                  httpRsp.GetResponseStream().Close()
              End If
  </code></pre>
  
  <p>Any tips or links to blogs with sample code would be great.
  Thanks.</p>