1

我使用此代码安装自签名证书(用户必须确认安装)。

    // Constructor
    public MainPage()
    {
        this.Loaded += new RoutedEventHandler(MainPage_Loaded);

    }
    private async void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        try
        {
            StorageFolder packageLocation = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder certificateFolder = await packageLocation.GetFolderAsync("Certificates");
            StorageFile certificate = await certificateFolder.GetFileAsync("myCer.cer");

            await Launcher.LaunchFileAsync(certificate);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }

是否可以检查证书是否已安装,以便我不必在每次启动应用程序时都安装它?

4

2 回答 2

1

证书可以通过多种方式进行比较,但最常见的两种方式是

  • 按指纹
  • 按序列号和发行人
    • 使用 PKI 时必须是唯一的
    • 无需计算即可更快地进行比较
    • 只有在验证链信任时才能被信任。攻击者可以使用选定的序列号和颁发者名称生成自签名证书。

在代码中:

X509Certificate cert1 = /* your cert */;
X509Certificate cert2 = /* your other cert */;

// assuming you are validating pki chain
// X509Certificate compares the serial number and issuer
bool matchUsingSerialAndIssuer = cert1.Equals(cert2);

// otherwise
bool publicKeyIsIdentical = cert1.GetCertHashString() == cert2.GetCertHashString();
// or easier to read if using X509Certificate2 (Thumbprint calls GetCertHashString)
// bool publicKeyIsIdentical = cert1.Thumbprint == cert2.Thumbprint;
于 2013-08-11T18:46:20.733 回答
0

你为什么不尝试这样的事情来找到证书。还将此名称空间包含到您的项目 System.Security.Cryptography.X509Certificates 中;如果您不能使用 X509,您可以更改以下代码以使用不同类型的证书。

 private static X509Certificate2 GetCertificateFromStore(string certSN)
        {

            X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
            try
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection col = store.Certificates;

                foreach (var currCert in col)
                {
                    var currSN = currCert.SerialNumber;
                    if (certSN.ToUpperInvariant() == currSN)
                    {
                        return currCert; // you found it return it
                        break;
                    }

                }

                return null; // you didnt now install it...
            }
            finally
            {
                store.Close();
            }


        }
于 2013-02-22T21:28:52.110 回答