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.
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
set stringCompare = CreateObject("Chilkat.StringBuilder")
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Keep this configuration private, outside public web content and source control.
set config = CreateObject("Chilkat.JsonObject")
config.Strict = 1
success = config.LoadFile("qa_data/azure/keyvault.json")
If (success <> 1) Then
outFile.WriteLine("Could not load private Key Vault configuration.")
WScript.Quit
End If
success = stringCompare.SetString(config.StringOf("client_id"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_id") <> 1) Or (stringMatch1 = 1)) Then
outFile.WriteLine("Missing or invalid client_id.")
WScript.Quit
End If
success = stringCompare.SetString(config.StringOf("tenant_id"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch2 = 1)) Then
outFile.WriteLine("Missing or invalid tenant_id.")
WScript.Quit
End If
set validate5 = CreateObject("Chilkat.StringBuilder")
set alphabet5 = CreateObject("Chilkat.StringBuilder")
success = validate5.Append(config.StringOf("tenant_id"))
If (success <> 1) Then
outFile.WriteLine("Could not validate tenant GUID.")
WScript.Quit
End If
success = alphabet5.Append("0123456789abcdefABCDEF-")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate5.Length < 36) Or (validate5.Length > 36)) Then
outFile.WriteLine("Invalid tenant GUID length.")
WScript.Quit
End If
charIndex5 = 0
Do While (charIndex5 < validate5.Length)
If (alphabet5.Contains(validate5.GetRange(charIndex5,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in tenant GUID.")
WScript.Quit
End If
charIndex5 = charIndex5 + 1
Loop
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = 0
set request = CreateObject("Chilkat.HttpRequest")
request.HttpVerb = "GET"
set response = CreateObject("Chilkat.HttpResponse")
set result = CreateObject("Chilkat.JsonObject")
result.Strict = 1
set tokenUrl = CreateObject("Chilkat.StringBuilder")
success = tokenUrl.Append("https://login.microsoftonline.com/")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = tokenUrl.Append(config.StringOf("tenant_id"))
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
success = tokenUrl.Append("/oauth2/v2.0/token")
If (success <> 1) Then
outFile.WriteLine("Could not construct URL or identifier.")
WScript.Quit
End If
' Your trusted workload runtime must refresh this projected token file.
set projected = CreateObject("Chilkat.StringBuilder")
success = projected.LoadFile("qa_data/azure/federated-token.jwt","utf-8")
If (success <> 1) Then
outFile.WriteLine("Could not read projected workload token.")
WScript.Quit
End If
success = projected.Trim()
If (success <> 1) Then
outFile.WriteLine("Could not trim token.")
WScript.Quit
End If
If (projected.Length = 0) Then
outFile.WriteLine("Projected token is empty.")
WScript.Quit
End If
assertion = projected.GetAsString()
request.HttpVerb = "POST"
request.ContentType = "application/x-www-form-urlencoded"
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 = CreateObject("Chilkat.CkDateTime")
success = tokenExpires.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read time.")
WScript.Quit
End If
success = http.HttpReq(tokenUrl.GetAsString(),request,response)
If (success <> 1) Then
outFile.WriteLine("Token exchange failed; inspect diagnostics privately.")
WScript.Quit
End If
If (response.StatusCode <> 200) Then
outFile.WriteLine("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
WScript.Quit
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Invalid JSON response.")
WScript.Quit
End If
success = stringCompare.SetString(result.StringOf("access_token"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("access_token") <> 1) Or (stringMatch3 = 1)) Then
outFile.WriteLine("Missing or invalid access_token.")
WScript.Quit
End If
success = stringCompare.SetString(result.StringOf("token_type"))
stringMatch4 = stringCompare.ContentsEqual("Bearer",1)
If (stringMatch4 <> 1) Then
outFile.WriteLine("Expected Bearer token.")
WScript.Quit
End If
If ((result.JsonTypeOf("expires_in") <> 2)) Then
outFile.WriteLine("Missing token lifetime.")
WScript.Quit
End If
set validate8 = CreateObject("Chilkat.StringBuilder")
set alphabet8 = CreateObject("Chilkat.StringBuilder")
success = validate8.Append(result.StringOf("expires_in"))
If (success <> 1) Then
outFile.WriteLine("Could not validate token lifetime.")
WScript.Quit
End If
success = alphabet8.Append("0123456789")
If (success <> 1) Then
outFile.WriteLine("Could not initialize validation.")
WScript.Quit
End If
If ((validate8.Length < 1) Or (validate8.Length > 8)) Then
outFile.WriteLine("Invalid token lifetime length.")
WScript.Quit
End If
charIndex8 = 0
Do While (charIndex8 < validate8.Length)
If (alphabet8.Contains(validate8.GetRange(charIndex8,1,0),1) <> 1) Then
outFile.WriteLine("Invalid character in token lifetime.")
WScript.Quit
End If
charIndex8 = charIndex8 + 1
Loop
tokenLifetime = result.IntOf("expires_in")
If ((tokenLifetime <= 60) Or (tokenLifetime > 604800)) Then
outFile.WriteLine("Unexpected token lifetime.")
WScript.Quit
End If
success = tokenExpires.AddSeconds(tokenLifetime)
If (success <> 1) Then
outFile.WriteLine("Could not calculate expiration.")
WScript.Quit
End If
set tokenNow = CreateObject("Chilkat.CkDateTime")
success = tokenNow.SetFromCurrentSystemTime()
If (success <> 1) Then
outFile.WriteLine("Could not read time.")
WScript.Quit
End If
If (tokenExpires.DiffSeconds(tokenNow) <= 60) Then
outFile.WriteLine("Token expired before it could be saved.")
WScript.Quit
End If
set saved = CreateObject("Chilkat.JsonObject")
success = saved.UpdateString("access_token",result.StringOf("access_token"))
If (success <> 1) Then
outFile.WriteLine("Could not set access_token.")
WScript.Quit
End If
success = saved.UpdateString("expires_at",tokenExpires.GetAsTimestamp(0))
If (success <> 1) Then
outFile.WriteLine("Could not set expires_at.")
WScript.Quit
End If
success = saved.WriteFile("qa_data/tokens/azureKeyVaultToken.json")
If (success <> 1) Then
outFile.WriteLine("Could not save the private token file.")
WScript.Quit
End If
outFile.WriteLine("Example completed.")
outFile.Close