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

Azure Key Vault: Sign a SOAP Body with XMLDSig and SHA-256

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.

Uses SHA-256 for the reference and signature, exclusive canonicalization, and verifies the generated signature and reference before saving. This is XMLDSig, not XAdES: it has no XAdES qualifying properties. Adapt WS-Security conventions to your service; SOAP headers are not signed.

Microsoft reference

Chilkat C# Downloads

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;
}

//  Create the XML to be signed.
Chilkat.StringBuilder sbXml = new Chilkat.StringBuilder();

bool bCrlf = true;
sbXml.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>",bCrlf);
sbXml.AppendLine("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">",bCrlf);
sbXml.AppendLine("    <SOAP-ENV:Header>",bCrlf);
sbXml.AppendLine("        <wsse:Security xmlns:wsse=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\" SOAP-ENV:mustUnderstand=\"1\"></wsse:Security>",bCrlf);
sbXml.AppendLine("    </SOAP-ENV:Header>",bCrlf);
sbXml.AppendLine("    <SOAP-ENV:Body xmlns:SOAP-SEC=\"http://schemas.xmlsoap.org/soap/security/2000-12\" SOAP-SEC:id=\"Body\">",bCrlf);
sbXml.AppendLine("        <z:FooBar xmlns:z=\"http://example.com\" />",bCrlf);
sbXml.AppendLine("    </SOAP-ENV:Body>",bCrlf);
sbXml.AppendLine("</SOAP-ENV:Envelope>",bCrlf);

Chilkat.XmlDSigGen gen = new Chilkat.XmlDSigGen();
gen.SigLocation = "SOAP-ENV:Envelope|SOAP-ENV:Header|wsse:Security";
gen.SignedInfoCanonAlg = "EXCL_C14N";
gen.SignedInfoDigestMethod = "sha256";
gen.SignedInfoPrefixList = "wsse SOAP-ENV";
gen.KeyInfoType = "X509Data";
gen.X509Type = "Certificate";
//  The reference selects the element with SOAP-SEC:id="Body".
success = gen.AddSameDocRef("Body","sha256","EXCL_C14N","","");
if (success != true) {
    Debug.WriteLine("Could not add the SOAP Body reference.");
    return;
}

success = gen.SetX509Cert(cert,true);
if (success != true) {
    Debug.WriteLine("Could not select signing certificate.");
    return;
}

success = gen.CreateXmlDSigSb(sbXml);
if (success != true) {
    Debug.WriteLine("XML signing failed; inspect diagnostics privately.");
    return;
}

//  Verify the signature and references before saving. This does not validate trust.
Chilkat.XmlDSig verifier = new Chilkat.XmlDSig();
success = verifier.LoadSignatureSb(sbXml);
if (success != true) {
    Debug.WriteLine("Could not load generated signature.");
    return;
}

success = verifier.VerifySignature(true);
if (success != true) {
    Debug.WriteLine("Generated XML signature or reference verification failed.");
    return;
}

success = sbXml.WriteFile("qa_output/azure_signed.xml","utf-8",false);
if (success != true) {
    Debug.WriteLine("Could not save signed XML.");
    return;
}

Debug.WriteLine("Verified XMLDSig saved to qa_output/azure_signed.xml");