Following is the code to sign the data. It works perfectly and signed when I run from Visual Studio but when I publish it to IIS it cannot match the certificate and no certificate gets assigned in mycert. Ultimately it throws the exception Cannot find the original signer. My development and publishing machine is the same. I have installed IIS and host the code on the same machine but no luck.
public static string Sign(string signData)
{
try
{
X509Certificate2 mycert = null;
X509Store my = new X509Store(StoreName.My, StoreLocation.CurrentUser);
my.Open(OpenFlags.ReadOnly);
foreach (X509Certificate2 cert in my.Certificates)
{
mycert = cert;
if (cert.Subject.ToUpper().Contains("MYNAME"))
{ // it never goes into this match when host in IIS
WriteToLogFile("*** Debugging mode at certificate match ***");
mycert = cert;
break;
}
}
string Base64Payload = Convert.ToBase64String(Encoding.UTF8.GetBytes(signData));
string sha256 = SHA256Checksum(signData);
byte[] data = Encoding.UTF8.GetBytes(sha256);
ContentInfo content = new ContentInfo(data);
SignedCms signedCms = new SignedCms(content);
CmsSigner signer = new CmsSigner(mycert);
signer.DigestAlgorithm = new Oid("SHA256");
//signer.IncludeOption = X509IncludeOption.WholeChain;
signer.IncludeOption = X509IncludeOption.EndCertOnly;
// create the signature
signedCms.ComputeSignature(signer, false); // Get Exception on this line Cannot find the original signer.
bool isValid = Verify(signedCms.Encode(), mycert);
return Convert.ToBase64String(signedCms.Encode());
}
catch (Exception ex)
{
WriteToLogFile("*** Exception while making signed data ***"+ex.Message);
throw ex;
}
}