29

How can I get X509Certificate from certificate store and then generate XML SignatureData in .net C#?

Perception
  • 79,279
  • 19
  • 185
  • 195
valisimo
  • 613
  • 2
  • 9
  • 14

1 Answers1

57

As far as I know, certificates are not saved by XML Format , you should combine it by yourself.

Is this what you want ?

   static void Main(string[] args)
   {
        X509Certificate2 cer = new X509Certificate2();
        cer.Import(@"D:\l.cer");
        X509Store store = new X509Store(StoreLocation.CurrentUser);
        store.Certificates.Add(cer);

        store.Open(OpenFlags.ReadOnly);
        X509Certificate2Collection cers = store.Certificates.Find(X509FindType.FindBySubjectName, "My Cert's Subject Name", false);
        if (cers.Count>0)
        {
            cer = cers[0];
        };
        store.Close();
   }
Dale K
  • 25,246
  • 15
  • 42
  • 71
Lost_Painting
  • 670
  • 6
  • 5
  • 39
    You need to call "store.Open(OpenFlags.ReadOnly);" before "Find" if the certificate wasn't added to the store before find. – aelstonjones Oct 09 '13 at 20:12
  • 1
    ```using System.Security.Cryptography.X509Certificates``` to get the namespace imported. – Aaron D Mar 21 '16 at 22:47