Azure Key Vault: Create a Self-Signed Certificate and Poll
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 HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.
Set cert_name and certificate_subject, such as CN=example.test. Creates a nonexportable RSA-2048 certificate key with 12-month validity and no key reuse. Requires certificates/create and certificates/get. HTTP 202 means accepted, not complete; polling is bounded to 30 requests and a 120-second checked deadline, with per-request timeouts. Issuer Self creates a self-signed certificate, unsuitable as a publicly trusted TLS identity. Retrieves the completed target and exports only its public DER.
Chilkat Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0
set stringCompare = Server.CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
set config = Server.CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not load private Key Vault configuration.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("vault_name"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid vault_name.") & "</pre>"
Response.End
End If
set validate1 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet1 = Server.CreateObject("Chilkat.StringBuilder")
success = validate1.Append(config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate vault_name.") & "</pre>"
Response.End
End If
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate1.Length < 3) Or (validate1.Length > 24)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid vault_name length.") & "</pre>"
Response.End
End If
charIndex1 = 0
Do While (charIndex1 < validate1.Length)
If (alphabet1.Contains(validate1.GetRange(charIndex1,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in vault_name.") & "</pre>"
Response.End
End If
charIndex1 = charIndex1 + 1
Loop
set vaultName = Server.CreateObject("Chilkat.StringBuilder")
success = vaultName.Append(config.StringOf("vault_name"))
set letters = Server.CreateObject("Chilkat.StringBuilder")
success = letters.Append("abcdefghijklmnopqrstuvwxyz")
If ((letters.Contains(vaultName.GetRange(0,1,0),1) <> 1) Or (vaultName.EndsWith("-",1) = 1) Or (vaultName.Contains("--",1) = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid vault_name.") & "</pre>"
Response.End
End If
set origin = Server.CreateObject("Chilkat.StringBuilder")
success = origin.Append("https://")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = origin.Append(config.StringOf("vault_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = origin.Append(".vault.azure.net")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = 0
ERROR: ASP example cannot use request or response as a variable name.
ERROR: ASP example cannot use request or response as a variable name.
ERROR: ASP example cannot use request or response as a variable name.
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1
' First run Get a Key Vault access token.
set token = Server.CreateObject("Chilkat.JsonObject")
token.Strict = 1
success = token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read the private token file.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(token.StringOf("access_token"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((token.JsonTypeOf("access_token") <> 1) Or (stringMatch2 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_token.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(token.StringOf("expires_at"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((token.JsonTypeOf("expires_at") <> 1) Or (stringMatch3 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid expires_at.") & "</pre>"
Response.End
End If
set expires = Server.CreateObject("Chilkat.CkDateTime")
set now = Server.CreateObject("Chilkat.CkDateTime")
success = expires.SetFromTimestamp(token.StringOf("expires_at"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid token expiration.") & "</pre>"
Response.End
End If
http.AuthToken = token.StringOf("access_token")
success = stringCompare.SetString(config.StringOf("cert_name"))
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("cert_name") <> 1) Or (stringMatch4 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid cert_name.") & "</pre>"
Response.End
End If
set validate40 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet40 = Server.CreateObject("Chilkat.StringBuilder")
success = validate40.Append(config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate cert_name.") & "</pre>"
Response.End
End If
success = alphabet40.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate40.Length < 1) Or (validate40.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid cert_name length.") & "</pre>"
Response.End
End If
charIndex40 = 0
Do While (charIndex40 < validate40.Length)
If (alphabet40.Contains(validate40.GetRange(charIndex40,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in cert_name.") & "</pre>"
Response.End
End If
charIndex40 = charIndex40 + 1
Loop
set resourceBase = Server.CreateObject("Chilkat.StringBuilder")
success = resourceBase.Append(origin.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = resourceBase.Append("/certificates/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = resourceBase.Append(config.StringOf("cert_name"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("certificate_subject"))
stringMatch5 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("certificate_subject") <> 1) Or (stringMatch5 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid certificate_subject.") & "</pre>"
Response.End
End If
set files = Server.CreateObject("Chilkat.FileAccess")
If (files.FileExists("qa_output/created-certificate.cer") = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Output exists; choose another private output path.") & "</pre>"
Response.End
End If
set body = Server.CreateObject("Chilkat.JsonObject")
success = body.UpdateString("policy.key_props.kty","RSA")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set policy.key_props.kty.") & "</pre>"
Response.End
End If
success = body.UpdateInt("policy.key_props.key_size",2048)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set key size.") & "</pre>"
Response.End
End If
success = body.UpdateBool("policy.key_props.exportable",0)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set exportability.") & "</pre>"
Response.End
End If
success = body.UpdateBool("policy.key_props.reuse_key",0)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set reuse policy.") & "</pre>"
Response.End
End If
success = body.UpdateString("policy.secret_props.contentType","application/x-pkcs12")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set policy.secret_props.contentType.") & "</pre>"
Response.End
End If
success = body.UpdateString("policy.x509_props.subject",config.StringOf("certificate_subject"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set policy.x509_props.subject.") & "</pre>"
Response.End
End If
success = body.UpdateInt("policy.x509_props.validity_months",12)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set validity.") & "</pre>"
Response.End
End If
success = body.UpdateString("policy.issuer.name","Self")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not set policy.issuer.name.") & "</pre>"
Response.End
End If
set createUrl = Server.CreateObject("Chilkat.StringBuilder")
success = createUrl.Append(resourceBase.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = createUrl.Append("/create?api-version=2026-04-01")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
Response.End
End If
If (expires.DiffSeconds(now) <= 60) Then
Response.Write "<pre>" & Server.HTMLEncode( "Token is expired or near expiration. Obtain a new token before continuing.") & "</pre>"
Response.End
End If
success = http.HttpJson("POST",createUrl.GetAsString(),body,"application/json",response)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request failed; reconcile uncertain writes before retrying.") & "</pre>"
Response.End
End If
If (response.StatusCode <> 202) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.") & "</pre>"
Response.End
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid JSON response.") & "</pre>"
Response.End
End If
set pendingUrl = Server.CreateObject("Chilkat.StringBuilder")
success = pendingUrl.Append(resourceBase.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = pendingUrl.Append("/pending?api-version=2026-04-01")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
set started = Server.CreateObject("Chilkat.CkDateTime")
set pollNow = Server.CreateObject("Chilkat.CkDateTime")
success = started.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not start polling deadline.") & "</pre>"
Response.End
End If
ready = 0
polls = 0
Do While ((ready <> 1) And (polls < 30))
polls = polls + 1
success = pollNow.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read time.") & "</pre>"
Response.End
End If
If (pollNow.DiffSeconds(started) >= 120) Then
Response.Write "<pre>" & Server.HTMLEncode( "Polling deadline reached; operation may still be pending. Resume GET polling, do not recreate.") & "</pre>"
Response.End
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
Response.End
End If
If (expires.DiffSeconds(now) <= 60) Then
Response.Write "<pre>" & Server.HTMLEncode( "Token is expired or near expiration. Obtain a new token before continuing.") & "</pre>"
Response.End
End If
ERROR: ASP example cannot use request or response as a variable name.
success = http.HttpReq(pendingUrl.GetAsString(),request,response)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request did not complete; inspect diagnostics privately.") & "</pre>"
Response.End
End If
If (response.StatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.") & "</pre>"
Response.End
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid JSON response.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("status"))
stringMatch6 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("status") <> 1) Or (stringMatch6 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid status.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("status"))
stringMatch7 = stringCompare.ContentsEqual("inProgress",1)
success = stringCompare.SetString(result.StringOf("status"))
stringMatch8 = stringCompare.ContentsEqual("completed",1)
If ((stringMatch7 <> 1) And (stringMatch8 <> 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Certificate operation failed, was cancelled, or returned an unknown status.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("status"))
stringMatch9 = stringCompare.ContentsEqual("completed",1)
If (stringMatch9 = 1) Then
ready = 1
End If
If (ready <> 1) Then
http.SleepMs 2000
End If
Loop
If (ready <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Poll limit reached; resume polling without repeating creation.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("target"))
stringMatch10 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("target") <> 1) Or (stringMatch10 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid target.") & "</pre>"
Response.End
End If
set certPrefix = Server.CreateObject("Chilkat.StringBuilder")
success = certPrefix.Append(resourceBase.GetAsString())
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = certPrefix.Append("/")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
set createdVersionId = Server.CreateObject("Chilkat.StringBuilder")
success = createdVersionId.Append(result.StringOf("target"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
If (createdVersionId.StartsWith(certPrefix.GetAsString(),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Returned identifier is outside the expected vault/resource.") & "</pre>"
Response.End
End If
createdVersion = createdVersionId.GetRange(certPrefix.Length,createdVersionId.Length - certPrefix.Length,0)
set validate41 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet41 = Server.CreateObject("Chilkat.StringBuilder")
success = validate41.Append(createdVersion)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate resource identifier.") & "</pre>"
Response.End
End If
success = alphabet41.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate41.Length < 1) Or (validate41.Length > 127)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid resource identifier length.") & "</pre>"
Response.End
End If
charIndex41 = 0
Do While (charIndex41 < validate41.Length)
If (alphabet41.Contains(validate41.GetRange(charIndex41,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in resource identifier.") & "</pre>"
Response.End
End If
charIndex41 = charIndex41 + 1
Loop
completedId = result.StringOf("target")
set createdUrl = Server.CreateObject("Chilkat.StringBuilder")
success = createdUrl.Append(completedId)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = createdUrl.Append("?api-version=2026-04-01")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
Response.End
End If
success = now.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
Response.End
End If
If (expires.DiffSeconds(now) <= 60) Then
Response.Write "<pre>" & Server.HTMLEncode( "Token is expired or near expiration. Obtain a new token before continuing.") & "</pre>"
Response.End
End If
ERROR: ASP example cannot use request or response as a variable name.
success = http.HttpReq(createdUrl.GetAsString(),request,response)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Request did not complete; inspect diagnostics privately.") & "</pre>"
Response.End
End If
If (response.StatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.") & "</pre>"
Response.End
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid JSON response.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("id"))
stringMatch11 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("id") <> 1) Or (stringMatch11 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid id.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("id"))
stringMatch12 = stringCompare.ContentsEqual(completedId,1)
If (stringMatch12 <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Completed certificate response ID mismatch.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("cer"))
stringMatch13 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("cer") <> 1) Or (stringMatch13 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid cer.") & "</pre>"
Response.End
End If
set certificate = Server.CreateObject("Chilkat.Cert")
success = certificate.LoadFromBase64(result.StringOf("cer"))
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid certificate.") & "</pre>"
Response.End
End If
success = certificate.ExportCertDerFile("qa_output/created-certificate.cer")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not export public certificate.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Example completed.") & "</pre>"
%>
</body>
</html>