I created a Digital Certificate and installed into the Certificate Store of the Local Machine. On .aspx page, the certificate is read from the store and the Digital Certificate is to be created with the Payload Information.
Below is the code written for the purpose.
postDocument.PreserveWhitespace = false;
System.Security.Cryptography.
Xml.SignedXml signedXml = new System.Security.Cryptography.
Xml.SignedXml(postDoc ument);
// Add a KeyInfo.
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(cert));
signedXml.KeyInfo = keyInfo;
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationWithCommentsUrl;
signedXml.SigningKey = cert.Key;
// Create a data object to hold the data to sign.
////System.Security.Cryptography.
Xml.DataObject dataObject = new System.Security.Cryptography.
Xml.DataObject();
////dataObject.Data = postDocument.SelectNodes("UserInfo");
////dataObject.Id = "MyObjectId";
// Add the data object to the signature.
////signedXml.AddObject(dataObject);
// Create a reference to be able to package everything into the message.
System.Security.Cryptography.
Xml.Reference reference = new System.Security.Cryptography.
Xml.Reference();
////reference.Uri = "#MyObjectId";
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
// Add it to the message.
signedXml.AddReference(reference);
// Compute the signature.
signedXml.ComputeSignature();
This code throws an error at line - signedXml.SigningKey = cert.Key;
The error stack is -
Server Error in '/CertificateStore' Application.
--------------------------------------------------------------------------------
Cannot find the certificate and private key for decryption
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: Cannot find the certificate and private key for decryption
Source Error:
Line 25: signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationWithCommentsUrl;
Line 26:
Line 27: signedXml.SigningKey = cert.Key;
Line 28:
Line 29: // Create a data object to hold the data to sign.
Source File: c:\Inetpub\wwwroot\CertificateStore\App_Code\gener ateSignature.cs Line: 27
Stack Trace:
[Win32Exception (0x80004005): Cannot find the certificate and private key for decryption]
Microsoft.Web.Services.Security.X509.X509Certifica te.get_Key() +571
CertificateStore.SignatureGenerator.getSignature(X 509Certificate cert, XmlDocument postDocument) in c:\Inetpub\wwwroot\CertificateStore\App_Code\gener ateSignature.cs:27
CertificateStore.SignInAction.signIt() in c:\Inetpub\wwwroot\CertificateStore\App_Code\SignI nAction.cs:31
_Default.Button1_Click(Object sender, EventArgs e) in c:\Inetpub\wwwroot\CertificateStore\TestUI.aspx.cs :33
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEven t(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42;
ASP.NET Version:2.0.50727.42
The application is built on .NET 2005.
The same code runs perfectly fine on Windows Application. But throws an error on
ASP.NET
Please advice. Its very critical for me.