如何使用C#或VB读取SSL证书的序列号?

时间:2016-03-26 15:02:19

标签: c# vb.net ssl ssl-certificate

我正在开展一个项目,我需要能够将预期的SSL证书序列号与我从服务器检索的证书的实际序列号进行比较。我怎么能在C#或VB中做到这一点?

2 个答案:

答案 0 :(得分:0)

您应该使用x509certificate2 class:

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

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("Url of the site");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
var cerificateFromClient = request.ServicePoint.Certificate;   

X509Certificate2 certificateFromServer = new X509Certificate2("path to certificate", "Passwod");
var serialFromClient = cerificateFromClient.GetSerialNumberString();
var serialFromServer = certificateFromServer.GetSerialNumberString();  

//Checking if both the serial numbers are same
bool isSame = string.Equals(serialFromClient, serialFromClient);

答案 1 :(得分:0)

只需使用X509Certificate2.SerialNumber财产。