This ASP.NET example searches the Current User certificate store and the Local Machine certificate store (from the ASPNET account) for a certificate matching an email address. If found, it checks to see if the private key is available, and if so, it tries to create a digital signature using the certificate and private key:
<font size=2 face=courier>
// Find a certificate matching an email address.
// Check to see if it has a private key available, and
// if it can be used to create a digital signature.
Chilkat.CreateCS ccs = new Chilkat.CreateCS();
// First check the Current User certificate store, then the
// Local Machine cert store.
Chilkat.CertStore cs = ccs.OpenCurrentUserStore();
Chilkat.Cert cert = cs.FindCertBySubjectE("support@chilkatsoft.com");
if (cert == null)
{
cs = ccs.OpenLocalSystemStore();
cert = cs.FindCertBySubjectE("support@chilkatsoft.com");
if (cert == null)
{
TextBox1.Text = "No matching certificate found!";
return;
}
}
// Do we have a private key?
if (cert.HasPrivateKey())
{
// Let’s use Chilkat Crypt to create the signature.
Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
crypt.UnlockComponent("30-day trial");
// Use this certificate for signing.
crypt.SetSigningCert(cert);
// The signature will be returned in base64 format.
crypt.EncodingMode = "base64″;
string sig = crypt.SignStringENC("This string will be signed");
if (sig.Length == 0)
{
TextBox4.Text = crypt.LastErrorText;
}
else
{
TextBox1.Text = "Create Digital Signature!";
}
}
else
{
TextBox1.Text = "Cannot create signature without a private key!";
}
</font>