By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,616 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,616 IT Pros & Developers. It's quick & easy.

help me Certificate Verification (C#)

P: n/a
I am trying to access an Web. The problem is that they
expect the client to submit a certificate. I can access the site from
my browser, but it always asks which certificate I want to use. How
can I do this in my code.

So far I can get the certificate. It's properly
formatted, and I can instantiate a X509Certificate object
with it. But I can't figure out how to add it to the Root
store, so it is trusted. Even if I add it to the Root
with IE, I still get the "The underlying connection was
closed: Could not establish trust relationship with remote
server." error message, when I connect to the HTTPS
resource via C#.

here is my code:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class CertPolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest request, int problem)
{
return true;
}
}

public class CertTest
{

public static void Main()
{
try
{
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("https://myweb/");
objRequest.Method = "POST";
X509Certificate myCert =
X509Certificate.CreateFromCertFile(@"d:\a\cert.der ");
X509CertificateCollection x509 = objRequest.ClientCertificates;
x509.Add (myCert);
objRequest.ClientCertificates.Add(myCert);
ServicePointManager.CertificatePolicy = new CertPolicy();

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
}
catch( Exception e )
{
Console.WriteLine( e.ToString() );
}
}
}
Thanks for any help
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
mb****@o2.pl (borovA) wrote in message news:<18**************************@posting.google. com>...
I am trying to access an Web. The problem is that they
expect the client to submit a certificate. I can access the site from
my browser, but it always asks which certificate I want to use. How
can I do this in my code.

So far I can get the certificate. It's properly
formatted, and I can instantiate a X509Certificate object
with it. But I can't figure out how to add it to the Root
store, so it is trusted. Even if I add it to the Root
with IE, I still get the "The underlying connection was
closed: Could not establish trust relationship with remote
server." error message, when I connect to the HTTPS
resource via C#.

here is my code:

using System;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class CertPolicy : ICertificatePolicy
{
public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,
WebRequest request, int problem)
{
return true;
}
}

public class CertTest
{

public static void Main()
{
try
{
HttpWebRequest objRequest =
(HttpWebRequest)WebRequest.Create("https://myweb/");
objRequest.Method = "POST";
X509Certificate myCert =
X509Certificate.CreateFromCertFile(@"d:\a\cert.der ");
X509CertificateCollection x509 = objRequest.ClientCertificates;
x509.Add (myCert);
objRequest.ClientCertificates.Add(myCert);
ServicePointManager.CertificatePolicy = new CertPolicy();

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
}
catch( Exception e )
{
Console.WriteLine( e.ToString() );
}
}
}
Problem was trivial :-)

solution:
objRequest.ContentLength = 0;



Thanks for any help

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.