How to send a client certificate using HttpWebRequest and IISExpress for debugging

I have code to extract a client certificate from a page request. This is the code in "MyPage.aspx".

string someparam = this.Request.Params["someparam"];
if (!String.IsNullOrWhiteSpace(someparam) && this.Page.Request.ClientCertificate.IsPresent)
{
    try
    {
        X509Certificate2 x509Cert2 = new X509Certificate2(this.Page.Request.ClientCertificate.Certificate);
        //
        // Code for verifying the x509Cert2
        //
    }
    catch (Exception) { }
}

Now I want to test the code in Visual Studio in my local environment. To do this, I installed IISExpress to get reliable https communication. So far, so good. The problem is that my test code for sending a client certificate does not work. ClientCertificate.IsPresent = falseon server.

Below is the test code. The certificate is a .pfx file.

var request = (HttpWebRequest)WebRequest.Create("https://localhost:44300/MyPage.aspx?someparam=222");
request.Method = "POST";

X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySerialNumber, "XXXTHESERIALNUMBERXXX", true);
certFromStore = col[0];
store.Close();
request.ClientCertificates.Add(certFromStore);

HttpWebResponse reqWebResponse = (HttpWebResponse)request.GetResponse();
StreamReader reqResponseStream = new StreamReader(reqWebResponse.GetResponseStream(), Encoding.UTF8);

String resultHtml = reqResponseStream.ReadToEnd();

reqWebResponse.Close();
reqResponseStream.Close();

I get no errors when running test code. The certificate is downloaded correctly from the store and successfully added to the request.ClientCertificates collection. But in MyPage.aspx, no certificate can be extracted from the page request.

- , ?

+5
1

IIS Express,
C:\Users\[username]\Documents\IISExpress\config\applicationhost.config

.

, IIS . enabled true :

iisClientCertificateMappingAuthentication enabled="true"

. sslFlags , . - , IIS , - sslFlags SslNegotiateCert :

access sslFlags="SslNegotiateCert"

+1

All Articles