title: Strange issue when resolving server SSL certificate within UnitTest in Visual Studio
body: <p>I have a really strange issue when Visual Studio resolves the server SSL certificate.</p>
  
  <p>The API I am using requires me to send a certificate along with the request to 'verify who I am'.  Initially I referred to this certificate within my UnitTest using <code>X509Certificate2</code> and then importing it as a <code>byte[]</code>.  What I didn't want to do, is have this laying around in my code referring to a specific location on my local disk (I want to run the unit tests automatically on my build server) so opted to install the certificate within the certificate store (which I could later install on the build server). And when I tested this Uri within the browser (IE) it asked for the certificate I wanted to use and rather stupidly I clicked OK without really reading what it was asking me to do.</p>
  
  <p>Problem is, since doing this I am unable to run my unit test (I am making an <code>HttpWebRequest</code>) - as soon as it hits the following line it throws a <code>WebException</code>.</p>
  
  <pre><code>using (var response = request.GetResponse()) 
  {
      // Removed content ...
  }
  </code></pre>
  
  <p>The exception response is null , here is the exception</p>
  
  <blockquote>
    <p>System.Net.WebException: The underlying connection was closed: An
    unexpected error occurred on a receive. ---> System.IO.IOException:
    Unable to read data from the transport connection: An existing
    connection was forcibly closed by the remote host. --->
    System.Net.Sockets.SocketException: An existing connection was
    forcibly closed by the remote host</p>
  </blockquote>
  
  <p>The only reason I can see for this error is that Visual Studio is using the wrong SSL certificate for the server connection (as I am using the test API Uri, I can verify this is sort of the case by using the live API Uri which works fine)</p>
  
  <p>I have tried the following:</p>
  
  <ul>
  <li>Removing the certificate from the store</li>
  <li>Adding the certificate back to the store</li>
  <li>Clearing all browser cache</li>
  <li>Restoring IE back to 'factory settings'</li>
  <li>Clearing the SSL cache within IE</li>
  <li>Tried setting Keepalive to true and then false</li>
  </ul>
  
  <p>This issue was affecting all browsers but I have managed to get it working.  I have tried to use Fiddler to identify if the server is doing something crazy (including decrypting SSL connections) and I can see this is actually responding with a 403 error.</p>
  
  <p>Has anyone else got any ideas?</p>
  
  <p>Edit</p>
  
  <p>Just tried to run this on the build server, still get the same error so I will check with the API provider to see if a/ my certificate has expired b/ their service is actually up!</p>
  
  <blockquote>
    <p>System.Net.WebException: The underlying connection was closed: An
    unexpected error occurred on a receive. ---> System.IO.IOException:
    Unable to read data from the transport connection: An existing
    connection was forcibly closed by the remote host. --->
    System.Net.Sockets.SocketException: An existing connection was
    forcibly closed by the remote host</p>
  </blockquote>