2015-04-22 97 views
0

我可以簡單地用此代碼顯示證書。我的問題是,我如何在證書中存儲或寫入證書?通過C#獲得網站證書

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

//Do webrequest to get info on secure site 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://mail.google.com"); 
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(); 

//display the cert dialog box 
X509Certificate2UI.DisplayCertificate(cert2); 
+0

您可以調用cert.Export(...)到一個字節[]並將其寫入文件。請參閱https://msdn.microsoft.com/en-us/library/dxz81eb9%28v=vs.110%29.aspx – Biscuits

+0

我應該如何填寫參數? ,我沒有密碼或其他東西。 – ccca

+0

它的工作原理,謝謝。 – ccca

回答

-1

看看的的X509Store類:

https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509store%28v=vs.110%29.aspx

沒有爲如何使用它(重複這裏)一個不錯的樣品有:

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

public class X509store2 
{ 
    public static void Main (string[] args) 
    { 
     //Create new X509 store called teststore from the local certificate store. 
     X509Store store = new X509Store ("teststore", StoreLocation.CurrentUser); 
     store.Open (OpenFlags.ReadWrite); 
     X509Certificate2 certificate = new X509Certificate2(); 

     //Create certificates from certificate files. 
     //You must put in a valid path to three certificates in the following constructors. 
     X509Certificate2 certificate1 = new X509Certificate2 ("c:\\mycerts\\*****.cer"); 
     X509Certificate2 certificate2 = new X509Certificate2 ("c:\\mycerts\\*****.cer"); 
     X509Certificate2 certificate5 = new X509Certificate2 ("c:\\mycerts\\*****.cer"); 

     //Create a collection and add two of the certificates. 
     X509Certificate2Collection collection = new X509Certificate2Collection(); 
     collection.Add (certificate2); 
     collection.Add (certificate5); 

     //Add certificates to the store. 
     store.Add (certificate1); 
     store.AddRange (collection); 

     X509Certificate2Collection storecollection = (X509Certificate2Collection)store.Certificates; 
     Console.WriteLine ("Store name: {0}", store.Name); 
     Console.WriteLine ("Store location: {0}", store.Location); 
     foreach (X509Certificate2 x509 in storecollection) 
     { 
      Console.WriteLine("certificate name: {0}",x509.Subject); 
     } 

     //Remove a certificate. 
     store.Remove (certificate1); 
     X509Certificate2Collection storecollection2 = (X509Certificate2Collection)store.Certificates; 
     Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine); 
     foreach (X509Certificate2 x509 in storecollection2) 
     { 
      Console.WriteLine ("certificate name: {0}", x509.Subject); 
     } 

     //Remove a range of certificates. 
     store.RemoveRange (collection); 
     X509Certificate2Collection storecollection3 = (X509Certificate2Collection)store.Certificates; 
     Console.WriteLine ("{1}Store name: {0}", store.Name, Environment.NewLine); 
     if (storecollection3.Count == 0) 
     { 
      Console.WriteLine ("Store contains no certificates."); 
     } 
     else 
     { 
      foreach (X509Certificate2 x509 in storecollection3) 
      { 
       Console.WriteLine ("certificate name: {0}", x509.Subject); 
      } 
     } 

     //Close the store. 
     store.Close(); 
    } 
} 
0

你可以請撥打cert.Export(...)以獲得您可以寫入文件的byte[]