如何使用.net core删除pfx证书

How to delete pfx certificate using .net core

我已经创建了删除 pfx 证书的代码,如下所示,

X509Store store = new X509Store(
                    StoreName.Root, StoreLocation.LocalMachine
                );

store.Open(OpenFlags.ReadOnly);
                            X509Certificate2Collection collection =
                                (X509Certificate2Collection)store.Certificates;

                            X509Certificate2Collection fcollection =
                                (X509Certificate2Collection)collection.Find(
                                    X509FindType.FindBySubjectName,
                                    "CN=AWS IoT Certificate", false
                                );


                            foreach (X509Certificate2 x509 in fcollection)
                            {
                                byte[] rawdata = x509.RawData;
                                Console.WriteLine(
                                    "Friendly Name: {0}{1}",
                                    x509.FriendlyName, Environment.NewLine
                                );
                                Console.WriteLine(
                                    "Simple Name: {0}{1}",
                                    x509.GetNameInfo(X509NameType.SimpleName, true),
                                    Environment.NewLine
                                );
                                store.Remove(x509);
                                Console.WriteLine(
                                    "X509Certificate2 for localhost removed."
                                );
                            }
                            store.Close();

但在 fcollection 变量中,没有获得带有主题名称的证书集合。 这里=>

X509Certificate2Collection fcollection =
                                    (X509Certificate2Collection)collection.Find(
                                        X509FindType.FindBySubjectName,
                                        "CN=AWS IoT Certificate", false
                                    );

如何获取证书集合。收集计数变为零。

X509FindType.FindBySubjectName 需要没有 RDN 属性的主题名称:"CN=AWS IoT Certificate" -> "AWS IoT Certificate"。并确保证书是否确实在商店中。

另外:

store.Open(OpenFlags.ReadOnly)

您正在以只读模式打开商店。您不能使用此标志修改商店。