how to create a completely new x509Certificate2 in .net?

后端 未结 6 1799
忘了有多久
忘了有多久 2020-12-14 08:07

I google it from web, find many samples to generate a new x509Certificate2 from a file in .net, but there is no one sample to show how to generate a completely new x509Certi

6条回答
  •  悲&欢浪女
    2020-12-14 08:12

    public X509Certificate2 GetCertificate()
    {
        var config = InitConfiguration();
        var certificateSubject = "X509Subject";
        var certificateStoreName = "X509StoreName";
        var certificateStoreLocation = "X509StoreLocation";
        var thumbPrint = "ThumbPrint";
    
        var storeName = (StoreName)Enum.Parse(typeof(StoreName), certificateStoreName, true);
        var storeLocation = (StoreLocation)Enum.Parse(typeof(StoreLocation), certificateStoreLocation, true);
    
        var certificateStore = new X509Store(storeName, storeLocation);
        certificateStore.Open(OpenFlags.ReadOnly);
    
        foreach (var storeCertificate in certificateStore.Certificates)
        {
            if (storeCertificate.Thumbprint.ToLower(System.Globalization.CultureInfo.CurrentCulture) == thumbPrint.ToLower(System.Globalization.CultureInfo.CurrentCulture))
            {return storeCertificate;
            }
        }
    certificateStore.Close();
        return null;
    }
    

提交回复
热议问题