Sample code for 30+ languages & platforms
Classic ASP Requires Chilkat v11.6.0+

Azure Key Vault: Authenticate with a PS256 Certificate Assertion

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.

Register the public certificate as an application credential, then provide client_id, tenant_id and client_pfx_password. The authentication certificate is separate from certificates stored in the vault. Signs a short-lived PS256 JWT with x5t#S256, exact tenant token-endpoint audience, issuer/subject client ID and a random jti. Requires synchronized time. The secret-free exchange still requires protecting the local private key and returned token. Does not print the assertion.

Microsoft reference

Chilkat Classic ASP Downloads

Classic ASP
<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("client_id"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_id") <> 1) Or (stringMatch1 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid client_id.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(config.StringOf("tenant_id"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch2 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid tenant_id.") & "</pre>"
    Response.End
End If

set validate5 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet5 = Server.CreateObject("Chilkat.StringBuilder")
success = validate5.Append(config.StringOf("tenant_id"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not validate tenant GUID.") & "</pre>"
    Response.End
End If

success = alphabet5.Append("0123456789abcdefABCDEF-")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
    Response.End
End If

If ((validate5.Length < 36) Or (validate5.Length > 36)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid tenant GUID length.") & "</pre>"
    Response.End
End If

charIndex5 = 0
Do While (charIndex5 < validate5.Length)
    If (alphabet5.Contains(validate5.GetRange(charIndex5,1,0),1) <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in tenant GUID.") & "</pre>"
        Response.End
    End If

    charIndex5 = charIndex5 + 1
Loop
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
set tokenUrl = Server.CreateObject("Chilkat.StringBuilder")
success = tokenUrl.Append("https://login.microsoftonline.com/")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
    Response.End
End If

success = tokenUrl.Append(config.StringOf("tenant_id"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
    Response.End
End If

success = tokenUrl.Append("/oauth2/v2.0/token")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not construct URL or identifier.") & "</pre>"
    Response.End
End If

If (config.JsonTypeOf("client_pfx_password") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing client_pfx_password string.") & "</pre>"
    Response.End
End If

set clientCert = Server.CreateObject("Chilkat.Cert")
success = clientCert.LoadPfxFile("qa_data/azure/client-auth.pfx",config.StringOf("client_pfx_password"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not load client authentication PFX.") & "</pre>"
    Response.End
End If

If (clientCert.IsRsa() <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "PS256 client assertion requires RSA.") & "</pre>"
    Response.End
End If

set signingKey = Server.CreateObject("Chilkat.PrivateKey")
success = clientCert.GetPrivateKey(signingKey)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Client certificate needs a private key.") & "</pre>"
    Response.End
End If

set certificateDer = Server.CreateObject("Chilkat.BinData")
success = certificateDer.AppendEncoded(clientCert.GetEncoded(),"base64")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not encode client certificate.") & "</pre>"
    Response.End
End If

thumbprint = certificateDer.GetHash("sha256","base64url")
If (certificateDer.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not hash certificate.") & "</pre>"
    Response.End
End If

set header = Server.CreateObject("Chilkat.JsonObject")
set claims = Server.CreateObject("Chilkat.JsonObject")
set jwt = Server.CreateObject("Chilkat.Jwt")
set random = Server.CreateObject("Chilkat.Prng")
jwtId = random.GenRandom(32,"hex")
If (random.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not generate assertion ID.") & "</pre>"
    Response.End
End If

success = header.UpdateString("alg","PS256")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set alg.") & "</pre>"
    Response.End
End If

success = header.UpdateString("typ","JWT")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set typ.") & "</pre>"
    Response.End
End If

success = header.UpdateString("x5t#S256",thumbprint)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set x5t#S256.") & "</pre>"
    Response.End
End If

success = claims.UpdateString("iss",config.StringOf("client_id"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set iss.") & "</pre>"
    Response.End
End If

success = claims.UpdateString("sub",config.StringOf("client_id"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set sub.") & "</pre>"
    Response.End
End If

success = claims.UpdateString("aud",tokenUrl.GetAsString())
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set aud.") & "</pre>"
    Response.End
End If

success = claims.UpdateString("jti",jwtId)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set jti.") & "</pre>"
    Response.End
End If

success = claims.UpdateInt("nbf",jwt.GenNumericDate(-30))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set assertion start.") & "</pre>"
    Response.End
End If

success = claims.UpdateInt("iat",jwt.GenNumericDate(0))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set assertion issue time.") & "</pre>"
    Response.End
End If

success = claims.UpdateInt("exp",jwt.GenNumericDate(300))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set assertion expiration.") & "</pre>"
    Response.End
End If

assertion = jwt.CreateJwtPk(header.Emit(),claims.Emit(),signingKey)
If (jwt.LastMethodSuccess <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not sign the client assertion.") & "</pre>"
    Response.End
End If

ERROR: ASP example cannot use request or response as a variable name.
ERROR: ASP example cannot use request or response as a variable name.
request.AddParam "client_id",config.StringOf("client_id")
request.AddParam "scope","https://vault.azure.net/.default"
request.AddParam "grant_type","client_credentials"
request.AddParam "client_assertion_type","urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
request.AddParam "client_assertion",assertion
set tokenExpires = Server.CreateObject("Chilkat.CkDateTime")
success = tokenExpires.SetFromCurrentSystemTime()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read time.") & "</pre>"
    Response.End
End If

success = http.HttpReq(tokenUrl.GetAsString(),request,response)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Token exchange failed; 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("access_token"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("access_token") <> 1) Or (stringMatch3 = 1)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_token.") & "</pre>"
    Response.End
End If

success = stringCompare.SetString(result.StringOf("token_type"))
stringMatch4 = stringCompare.ContentsEqual("Bearer",1)
If (stringMatch4 <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected Bearer token.") & "</pre>"
    Response.End
End If

If ((result.JsonTypeOf("expires_in") <> 2)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing token lifetime.") & "</pre>"
    Response.End
End If

set validate7 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet7 = Server.CreateObject("Chilkat.StringBuilder")
success = validate7.Append(result.StringOf("expires_in"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not validate token lifetime.") & "</pre>"
    Response.End
End If

success = alphabet7.Append("0123456789")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
    Response.End
End If

If ((validate7.Length < 1) Or (validate7.Length > 8)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid token lifetime length.") & "</pre>"
    Response.End
End If

charIndex7 = 0
Do While (charIndex7 < validate7.Length)
    If (alphabet7.Contains(validate7.GetRange(charIndex7,1,0),1) <> 1) Then
        Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in token lifetime.") & "</pre>"
        Response.End
    End If

    charIndex7 = charIndex7 + 1
Loop
tokenLifetime = result.IntOf("expires_in")
If ((tokenLifetime <= 60) Or (tokenLifetime > 604800)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Unexpected token lifetime.") & "</pre>"
    Response.End
End If

success = tokenExpires.AddSeconds(tokenLifetime)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not calculate expiration.") & "</pre>"
    Response.End
End If

set tokenNow = Server.CreateObject("Chilkat.CkDateTime")
success = tokenNow.SetFromCurrentSystemTime()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read time.") & "</pre>"
    Response.End
End If

If (tokenExpires.DiffSeconds(tokenNow) <= 60) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Token expired before it could be saved.") & "</pre>"
    Response.End
End If

set saved = Server.CreateObject("Chilkat.JsonObject")
success = saved.UpdateString("access_token",result.StringOf("access_token"))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set access_token.") & "</pre>"
    Response.End
End If

success = saved.UpdateString("expires_at",tokenExpires.GetAsTimestamp(0))
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not set expires_at.") & "</pre>"
    Response.End
End If

success = saved.WriteFile("qa_data/tokens/azureKeyVaultToken.json")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not save the private token file.") & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Example completed.") & "</pre>"

%>
</body>
</html>