Get ssl -.net certificate information

I am looking to retrieve data from any domain SSL certificate. For example, I want to specify any website address, for example. " /qaru.site / ... " and my code first checks for an SSL certificate. If this happens, I want him to take out the certificate expiration date. [I am reading Domainnames from DB] Example: http://www.digicert.com/help/

I need to create a web service to check the expiration date. how can i implement this? - I looked through a lot of different things, such as RequestCertificateValidationCallback and ClientCertificates, etc. Since I'm new to this, I'm not sure what to do.

I could be completely right (so I need help), but I would create an HTTPWebRequest and then somehow request the client certificate and specific elements in this way?

I tried the example provided when obtaining the @SSL certificate pre-fetch.NET, but I am getting an invalid 403 error.

Any help would be greatly appreciated - Thanks.

This is the code I wrote that throws a 403 forbidden error.

Uri u = new Uri("http://services.efi.com/"); ServicePoint sp = ServicePointManager.FindServicePoint(u); string groupName = Guid.NewGuid().ToString(); HttpWebRequest req = HttpWebRequest.Create(u) as HttpWebRequest; req.Accept = "*/*"; req.ConnectionGroupName = groupName; using (WebResponse resp = req.GetResponse()) { // Ignore response, and close the response. } sp.CloseConnectionGroup(groupName); // Implement favourite null check pattern here on sp.Certificate string expiryDate = sp.Certificate.GetExpirationDateString(); string str = expiryDate; 
+7
source share
3 answers

You get the status "403 Forbidden" because this is what the server returns when accessing this page. I see the same when I view this Uri using IE. This status indicates that you do not have permission to access the URL, so perhaps you should try your code on the page that you have access to.

Also, you are unlikely to see a certificate in the http connection. Instead, you can try https .

+1
source

This works great:

 namespace ConsoleApplication1 { using System; using System.Net; using System.Net.Security; using System.Security.Cryptography.X509Certificates; class Program { static void Main() { ServicePointManager.ServerCertificateValidationCallback += ServerCertificateValidationCallback; var request = WebRequest.Create("https://www.google.com"); var response = request.GetResponse(); Console.WriteLine("Done."); Console.ReadLine(); } private static bool ServerCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { Console.WriteLine("Certificate expires on " + certificate.GetExpirationDateString()); return true; } } } 
+5
source

If you need to upload a certificate:

  //Do webrequest to get info on secure site var certName = "FileName"; var url = "https://mail.google.com"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Close(); //retrieve the ssl cert and assign it to an X509Certificate object X509Certificate cert = request.ServicePoint.Certificate; //convert the X509Certificate to an X509Certificate2 object by passing it into the constructor X509Certificate2 cert2 = new X509Certificate2(cert); string cn = cert2.GetIssuerName(); string cedate = cert2.GetExpirationDateString(); string cpub = cert2.GetPublicKeyString(); var path = Directory.GetCurrentDirectory() + string.Concat("\\", certName, ".der"); byte[] certData = cert2.Export(X509ContentType.Cert); File.WriteAllBytes(path, certData); Console.WriteLine("cert2.GetIssuerName :{0}", cert2.GetIssuerName()); Console.WriteLine("cert2.GetExpirationDateString :{0}", cert2.GetExpirationDateString()); Console.WriteLine("cert2.GetPublicKeyString :{0}", cert2.GetPublicKeyString()); 

.cs Sample file: https://gist.github.com/thedom85/6db200104c075310527aaef63b172253

I also recommend this site: https://www.simple-talk.com/dotnet/.net-framework/tlsssl-and-.net-framework-4.0/

+1
source

All Articles