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

Azure Key Vault: Exchange a Workload Identity Token

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.

Configure an Entra federated identity credential with the exact external issuer, subject, and audience (commonly api://AzureADTokenExchange). Provide client_id and tenant_id. Have the trusted workload runtime supply a fresh projected JWT at qa_data/azure/federated-token.jwt before each exchange; adapt this path to your runtime-provided token path. The application forwards this assertion to the fixed tenant endpoint; Entra validates the federation trust. This does not mint or locally trust an arbitrary external token.

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

'  Your trusted workload runtime must refresh this projected token file.
set projected = Server.CreateObject("Chilkat.StringBuilder")
success = projected.LoadFile("qa_data/azure/federated-token.jwt","utf-8")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read projected workload token.") & "</pre>"
    Response.End
End If

success = projected.Trim()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not trim token.") & "</pre>"
    Response.End
End If

If (projected.Length = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Projected token is empty.") & "</pre>"
    Response.End
End If

assertion = projected.GetAsString()
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 validate8 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet8 = Server.CreateObject("Chilkat.StringBuilder")
success = validate8.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 = alphabet8.Append("0123456789")
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
    Response.End
End If

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

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

    charIndex8 = charIndex8 + 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>