简体   繁体   English

访问自签名证书的详细信息

[英]Access the self signed certificate details

I have an asp.net mvc website deployed to iis. 我有一个部署到iis的asp.net mvc网站。 A self signed SSL certificate is used in order to secure the traffic. 自签名SSL证书用于保护流量。 I would like to access this self signed certificate from my asp.net, probably in the startup class or something, in order to get the validity of the self signed certificate (i need this metric for something else). 我想从我的asp.net(可能在startup类之类的东西中)访问此自签名证书,以便获得自签名证书的有效性(我需要此指标用于其他功能)。

How could i do that? 我该怎么办?

I would gladly post some code, or what I've tried so far, but sadly i have no clue where to start from! 我很乐意发布一些代码,或者到目前为止我已经尝试过的内容,但可惜我不知道从哪里开始!

I would really appreciate any help. 我真的很感谢您的帮助。

Edit 编辑

To rephrase my question, lets say i have an asp.net web service deployed to IIS, how do i access the certificates in that IIS, and retrieve their validity period (from with in the web service using c# code) 再说一遍我的问题,可以说我有一个部署到IIS的asp.net Web服务,我如何访问该IIS中的证书,并检索其有效期(使用C#代码从Web服务中获取)

You can do this by opening the cert store and finding certs based upon search criteria. 您可以通过打开证书存储区并根据搜索条件查找证书来做到这一点。 If it's a self signed cert that you created you should know something about it. 如果这是您创建的自签名证书,则应该对此有所了解。

object value = "AcmeOrganization";
X509FindType findType = X509FindType.FindByIssuerName;
StoreName storeName = StoreName.My;
StoreLocation storeLocation = StoreLocation.CurrentUser;
var store = new X509Store(storeName, storeLocation);
try
{
  store.Open(OpenFlags.ReadOnly);
  var certs = store.Certificates.Find(findType, value, true);
  if (certs.Count > 0) 
  {
    return certs[0];
  }
}
finally
{
  store.Close();
  store = null;
}

This will get you the cert you're looking for then you can call Verify which does chain validation. 这将为您提供所需的证书,然后可以致电验证进行链验证。 Other properties along with expiration will be available with the X509Certificate2 object. X509Certificate2对象将提供其他属性以及到期时间。

certs[0].Verify()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM