martedì 27 agosto 2024

AX 2012 - Lettura file di un certificato e stampa proprietà

 Questo semplice job mostra come leggere un file di un certificato e stampare le varie proprietà:

 static void LIL_CertificateInfo(Args _args)  
 {  
   int                               version;  
   utcDateTime                           NotBefore,NotAfter;  
   System.Security.Cryptography.X509Certificates.X509Certificate2 cert;  
   System.Security.Cryptography.X509Certificates.PublicKey     publicKey;  
   System.Security.Cryptography.Oid                oid;  
   System.Security.Cryptography.AsnEncodedData           asnEncodeddata;  
   System.Security.Cryptography.AsymmetricAlgorithm        AsymmetricAlgorithm;  
   
   cert = new System.Security.Cryptography.X509Certificates.X509Certificate2(System.IO.File::ReadAllBytes(@'C:\Temp\bob.crt'));  
        
   //oppure   
   //cert = new System.Security.Cryptography.X509Certificates.X509Certificate2();  
   //cert.Import(@'C:\Temp\myCert.pfx', 'password', System.Security.Cryptography.X509Certificates.X509KeyStorageFlags::DefaultKeySet);  
   
   version       = cert.get_Version();  
   NotBefore      = cert.get_NotBefore();  
   NotAfter      = cert.get_NotAfter();  
   publicKey      = cert.get_PublicKey();  
   oid         = publicKey.get_Oid();  
   asnEncodeddata   = publicKey.get_EncodedKeyValue();  
   AsymmetricAlgorithm = publicKey.get_Key();  
   
   info(cert.get_Subject());  
   info(cert.get_Issuer());  
   info(strFmt("%1",version));  
   info(strfmt("%1",NotBefore));  
   info(strfmt("%1",NotAfter));  
   info(cert.get_Thumbprint());  
   info(cert.get_SerialNumber());  
   info(oid.get_FriendlyName());  
   info(asnEncodeddata.Format(true));  
   info(cert.ToString(true));  
   info(AsymmetricAlgorithm.ToXmlString(false));  
 }