I am trying to sign a pdf document with a cert from a win store. I saw it here how to do it
I have something like:
X509Certificate2 certFromStore = …
ExternalSignature externalSignature = new ExternalSignature(certFromStore);
…
pdfSign.Sign(pdfDocument.Pages.Count, false, new System.Drawing.Rectangle(1, 1, 1, 1), pkcs);
I get “No private key”. I saw that u had some problems due to this. What I’m doing wrong? Is it even a right approach to sign document with a cert fom a win store?
A PFX file , also known as PKCS #12 , is a single, password protected certificate archive which is needed while signing PDF document digitally. You can further check details on how to get it over shared link.
Furthermore, if you are signing a PDF document with Smart Card, you should use the code snippet(s) given in the article which like you have shared in your first post. In case of any issue, we request you to please explain a bit more about the issue you are facing and in case you are using any .pfx file to sign PDF, please share it so that scenario can be tested accordingly.
I get user cert from request, in a form of a string (like: “96CB3E…”). Then
var userCertificate = new X509Certificate2(digitalSignature.StringToByteArray(request.SignerCertificate));
I go to the win store to check if there is a cert associated with the above one:
store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, findValue, true);
We were able to replicate the scenario as you have mentioned. Aspose.PDF provides a way to sign PDF document from winstore. However, the case where certificate comes in form of string needs to be investigated further. We have logged an investigation ticket as PDFNET-47526 in our issue tracking system for the purpose and will surely inform you as soon as some progress is made towards its resolution. Please spare us little time.
We will definitely address every issue which has been faced while running the scenario. However, would you please share how you receive this exception OR which code snippet causes this error?
As the issue has recently been logged in our issue tracking system, it is pending for analysis. However, we will let you know within this forum thread as soon as it is resolved.
MemoryStream memStream = new MemoryStream(pkcs);
PKCS1 result = new PKCS1(memStream, “tst”);
using MemoryStream ms = new MemoryStream();
pdfSign.Sign(1, “Signature Reason”, “Contact”, “Location”, false, new System.Drawing.Rectangle(1, 1, 1, 1), res);
And when you try to save it, this causes error (I also tried to save it in byte[], and I get the same error - and I need to save it into byte[])
pdfSign.Save(dataDir + “\test.pdf”);
When I remove password part from the code above, then I get:
Currently, the investigation against your ticket is underway and we will surely try to complete it as soon as possible. We have also recorded your concerns and will surely consider them during investigation of the ticket. We highly appreciate your patience and comprehension in this regard. Please spare us some time.
@ asad.ali - Hi, I actually resolved this. I didn’t realize it but the cert I was using did not have the private key in it as well. Once I changed certs I got it to work just fine.
It is good to know that you are able to resolve your issue. Please keep using our API and in case you need any further assistance, please feel free to let us know.