Sample code for 30+ languages & platforms
.NET Core C# Requires Chilkat v11.6.0+

Azure Key Vault: Create an Attached CAdES-BES Signature

See more Azure Key Vault Examples

Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.

Uses Chilkat's cloud convenience integration with client_id, client_secret, tenant_id (directory GUID), vault_name, and cert_name in private configuration. The installed Chilkat library manages authentication and service API versions for this helper; this example does not select a REST api-version. Use the explicit Http examples when that control is required. Keep the runtime current and inspect diagnostics privately.

Set explicit cert_version and load its matching public certificate from qa_data/certs/myCert.cer. Keep the version pinned rather than implicitly following certificate rotation. The backing key must permit signing, and the application needs certificates/get and keys/sign. The private key remains in Key Vault; the document is assembled locally and its digest is signed remotely. A cryptographically valid signature does not establish certificate trust, revocation status, or a trusted signing time.

Creates an attached .p7m with SHA-256 and signingCertificateV2. This example does not add a timestamp or long-term validation evidence.

Microsoft reference

Chilkat .NET Core C# Downloads

.NET Core C#
bool success = false;

Chilkat.StringBuilder stringCompare = new Chilkat.StringBuilder();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this configuration private, outside public web content and source control.
Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/keyvault.json");
if (success != true) {
    Debug.WriteLine("Could not load private Key Vault configuration.");
    return;
}

stringCompare.SetString(config.StringOf("vault_name"));
bool stringMatch1 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("vault_name") != 1) || (stringMatch1 == true)) {
    Debug.WriteLine("Missing or invalid vault_name.");
    return;
}

Chilkat.StringBuilder validate1 = new Chilkat.StringBuilder();
Chilkat.StringBuilder alphabet1 = new Chilkat.StringBuilder();
success = validate1.Append(config.StringOf("vault_name"));
if (success != true) {
    Debug.WriteLine("Could not validate vault_name.");
    return;
}

success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-");
if (success != true) {
    Debug.WriteLine("Could not initialize validation.");
    return;
}

if ((validate1.Length < 3) || (validate1.Length > 24)) {
    Debug.WriteLine("Invalid vault_name length.");
    return;
}

int charIndex1 = 0;
while ((charIndex1 < validate1.Length)) {
    if (alphabet1.Contains(validate1.GetRange(charIndex1,1,false),true) != true) {
        Debug.WriteLine("Invalid character in vault_name.");
        return;
    }

    charIndex1 = charIndex1 + 1;
}

Chilkat.StringBuilder vaultName = new Chilkat.StringBuilder();
vaultName.Append(config.StringOf("vault_name"));
Chilkat.StringBuilder letters = new Chilkat.StringBuilder();
letters.Append("abcdefghijklmnopqrstuvwxyz");
if ((letters.Contains(vaultName.GetRange(0,1,false),true) != true) || (vaultName.EndsWith("-",true) == true) || (vaultName.Contains("--",true) == true)) {
    Debug.WriteLine("Invalid vault_name.");
    return;
}

Chilkat.StringBuilder origin = new Chilkat.StringBuilder();
success = origin.Append("https://");
if (success != true) {
    Debug.WriteLine("Could not construct URL or identifier.");
    return;
}

success = origin.Append(config.StringOf("vault_name"));
if (success != true) {
    Debug.WriteLine("Could not construct URL or identifier.");
    return;
}

success = origin.Append(".vault.azure.net");
if (success != true) {
    Debug.WriteLine("Could not construct URL or identifier.");
    return;
}

stringCompare.SetString(config.StringOf("cert_name"));
bool stringMatch2 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("cert_name") != 1) || (stringMatch2 == true)) {
    Debug.WriteLine("Missing or invalid cert_name.");
    return;
}

Chilkat.StringBuilder validate2 = new Chilkat.StringBuilder();
Chilkat.StringBuilder alphabet2 = new Chilkat.StringBuilder();
success = validate2.Append(config.StringOf("cert_name"));
if (success != true) {
    Debug.WriteLine("Could not validate cert_name.");
    return;
}

success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
    Debug.WriteLine("Could not initialize validation.");
    return;
}

if ((validate2.Length < 1) || (validate2.Length > 127)) {
    Debug.WriteLine("Invalid cert_name length.");
    return;
}

int charIndex2 = 0;
while ((charIndex2 < validate2.Length)) {
    if (alphabet2.Contains(validate2.GetRange(charIndex2,1,false),true) != true) {
        Debug.WriteLine("Invalid character in cert_name.");
        return;
    }

    charIndex2 = charIndex2 + 1;
}

stringCompare.SetString(config.StringOf("cert_version"));
bool stringMatch3 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("cert_version") != 1) || (stringMatch3 == true)) {
    Debug.WriteLine("Missing or invalid cert_version.");
    return;
}

Chilkat.StringBuilder validate3 = new Chilkat.StringBuilder();
Chilkat.StringBuilder alphabet3 = new Chilkat.StringBuilder();
success = validate3.Append(config.StringOf("cert_version"));
if (success != true) {
    Debug.WriteLine("Could not validate cert_version.");
    return;
}

success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-");
if (success != true) {
    Debug.WriteLine("Could not initialize validation.");
    return;
}

if ((validate3.Length < 1) || (validate3.Length > 127)) {
    Debug.WriteLine("Invalid cert_version length.");
    return;
}

int charIndex3 = 0;
while ((charIndex3 < validate3.Length)) {
    if (alphabet3.Contains(validate3.GetRange(charIndex3,1,false),true) != true) {
        Debug.WriteLine("Invalid character in cert_version.");
        return;
    }

    charIndex3 = charIndex3 + 1;
}

stringCompare.SetString(config.StringOf("client_id"));
bool stringMatch4 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("client_id") != 1) || (stringMatch4 == true)) {
    Debug.WriteLine("Missing or invalid client_id.");
    return;
}

stringCompare.SetString(config.StringOf("client_secret"));
bool stringMatch5 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("client_secret") != 1) || (stringMatch5 == true)) {
    Debug.WriteLine("Missing or invalid client_secret.");
    return;
}

stringCompare.SetString(config.StringOf("tenant_id"));
bool stringMatch6 = stringCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("tenant_id") != 1) || (stringMatch6 == true)) {
    Debug.WriteLine("Missing or invalid tenant_id.");
    return;
}

Chilkat.StringBuilder validate4 = new Chilkat.StringBuilder();
Chilkat.StringBuilder alphabet4 = new Chilkat.StringBuilder();
success = validate4.Append(config.StringOf("tenant_id"));
if (success != true) {
    Debug.WriteLine("Could not validate tenant GUID.");
    return;
}

success = alphabet4.Append("0123456789abcdefABCDEF-");
if (success != true) {
    Debug.WriteLine("Could not initialize validation.");
    return;
}

if ((validate4.Length < 36) || (validate4.Length > 36)) {
    Debug.WriteLine("Invalid tenant GUID length.");
    return;
}

int charIndex4 = 0;
while ((charIndex4 < validate4.Length)) {
    if (alphabet4.Contains(validate4.GetRange(charIndex4,1,false),true) != true) {
        Debug.WriteLine("Invalid character in tenant GUID.");
        return;
    }

    charIndex4 = charIndex4 + 1;
}

Chilkat.Cert cert = new Chilkat.Cert();
success = cert.LoadFromFile("qa_data/certs/myCert.cer");
if (success != true) {
    Debug.WriteLine("Could not load the public signing certificate.");
    return;
}

Chilkat.JsonObject cloud = new Chilkat.JsonObject();
success = cloud.UpdateString("service","azure_keyvault");
if (success != true) {
    Debug.WriteLine("Could not set service.");
    return;
}

success = cloud.UpdateString("client_id",config.StringOf("client_id"));
if (success != true) {
    Debug.WriteLine("Could not set client_id.");
    return;
}

success = cloud.UpdateString("client_secret",config.StringOf("client_secret"));
if (success != true) {
    Debug.WriteLine("Could not set client_secret.");
    return;
}

success = cloud.UpdateString("tenant_id",config.StringOf("tenant_id"));
if (success != true) {
    Debug.WriteLine("Could not set tenant_id.");
    return;
}

success = cloud.UpdateString("vault_name",config.StringOf("vault_name"));
if (success != true) {
    Debug.WriteLine("Could not set vault_name.");
    return;
}

success = cloud.UpdateString("cert_name",config.StringOf("cert_name"));
if (success != true) {
    Debug.WriteLine("Could not set cert_name.");
    return;
}

success = cloud.UpdateString("cert_version",config.StringOf("cert_version"));
if (success != true) {
    Debug.WriteLine("Could not set cert_version.");
    return;
}

success = cert.SetCloudSigner(cloud);
if (success != true) {
    Debug.WriteLine("Could not configure the Key Vault signer; inspect diagnostics privately.");
    return;
}

Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
success = crypt.SetSigningCert(cert);
if (success != true) {
    Debug.WriteLine("Could not select signing certificate.");
    return;
}

crypt.CadesEnabled = true;
crypt.HashAlgorithm = "sha256";
Chilkat.JsonObject signedAttrs = new Chilkat.JsonObject();
success = signedAttrs.UpdateInt("contentType",1);
if (success != true) {
    Debug.WriteLine("Could not set signed attribute.");
    return;
}

success = signedAttrs.UpdateInt("signingTime",1);
if (success != true) {
    Debug.WriteLine("Could not set signed attribute.");
    return;
}

success = signedAttrs.UpdateInt("messageDigest",1);
if (success != true) {
    Debug.WriteLine("Could not set signed attribute.");
    return;
}

success = signedAttrs.UpdateInt("signingCertificateV2",1);
if (success != true) {
    Debug.WriteLine("Could not set signed attribute.");
    return;
}

crypt.SigningAttributes = signedAttrs.Emit();
//  The attached .p7m includes the original file. Key Vault signs the digest remotely.
success = crypt.CreateP7M("qa_data/e-Invoice.xml","qa_output/signed.p7m");
if (success != true) {
    Debug.WriteLine("CAdES signing failed; inspect diagnostics privately.");
    return;
}

Debug.WriteLine("Attached CAdES-BES signature saved to qa_output/signed.p7m");