Azure Key Vault: Get an OAuth2 Token with Client Credentials
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 the Microsoft Entra v2 tenant-specific token endpoint and https://vault.azure.net/.default scope. Set nonempty client_id, client_secret, and directory GUID tenant_id in the private configuration. Use the client secret VALUE, not its identifier. This is for confidential applications, never browser/mobile clients. Client credentials have no refresh token: request another access token when needed. The token file is plaintext and must be protected by OS permissions. Token issuance does not grant vault access; grant only the required data-plane permissions to the service principal through Azure RBAC or the vault's configured access-policy model. Contributor alone does not grant data-plane access. Prefer managed identity or workload federation where available.
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("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("client_secret"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("client_secret") <> 1) Or (stringMatch2 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid client_secret.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(config.StringOf("tenant_id"))
stringMatch3 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch3 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid tenant_id.") & "</pre>"
Response.End
End If
set validate4 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet4 = Server.CreateObject("Chilkat.StringBuilder")
success = validate4.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 = alphabet4.Append("0123456789abcdefABCDEF-")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate4.Length < 36) Or (validate4.Length > 36)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid tenant GUID length.") & "</pre>"
Response.End
End If
charIndex4 = 0
Do While (charIndex4 < validate4.Length)
If (alphabet4.Contains(validate4.GetRange(charIndex4,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in tenant GUID.") & "</pre>"
Response.End
End If
charIndex4 = charIndex4 + 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
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 "client_secret",config.StringOf("client_secret")
request.AddParam "scope","https://vault.azure.net/.default"
request.AddParam "grant_type","client_credentials"
' Use request-start time so network latency cannot extend the cached lifetime.
set expires = Server.CreateObject("Chilkat.CkDateTime")
success = expires.SetFromCurrentSystemTime()
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not read current time.") & "</pre>"
Response.End
End If
success = http.HttpReq(tokenUrl.GetAsString(),request,response)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Token request failed; inspect diagnostics privately.") & "</pre>"
Response.End
End If
If (response.StatusCode <> 200) Then
Response.Write "<pre>" & Server.HTMLEncode( "HTTP status: " & response.StatusCode) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Request ID: " & response.GetHeaderField("x-ms-request-id")) & "</pre>"
' Do not print full response bodies, tokens, or diagnostic logs.
Response.Write "<pre>" & Server.HTMLEncode( "Request failed. Inspect private diagnostics; reconcile writes before retrying.") & "</pre>"
Response.End
End If
success = result.Load(response.BodyStr)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a well-formed JSON response.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("access_token"))
stringMatch4 = stringCompare.ContentsEqual("",1)
If ((result.JsonTypeOf("access_token") <> 1) Or (stringMatch4 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_token.") & "</pre>"
Response.End
End If
success = stringCompare.SetString(result.StringOf("token_type"))
stringMatch5 = stringCompare.ContentsEqual("Bearer",1)
If (stringMatch5 <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected a Bearer token.") & "</pre>"
Response.End
End If
If (result.JsonTypeOf("expires_in") <> 2) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing numeric token lifetime.") & "</pre>"
Response.End
End If
lifetime = result.StringOf("expires_in")
set validate5 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet5 = Server.CreateObject("Chilkat.StringBuilder")
success = validate5.Append(lifetime)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not validate expires_in.") & "</pre>"
Response.End
End If
success = alphabet5.Append("0123456789")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate5.Length < 1) Or (validate5.Length > 8)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid expires_in 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 expires_in.") & "</pre>"
Response.End
End If
charIndex5 = charIndex5 + 1
Loop
seconds = result.IntOf("expires_in")
If ((seconds <= 60) Or (seconds > 604800)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Unexpected token lifetime.") & "</pre>"
Response.End
End If
success = expires.AddSeconds(seconds)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not compute expiration.") & "</pre>"
Response.End
End If
set now = Server.CreateObject("Chilkat.CkDateTime")
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
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",expires.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 token to the private token file.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Access token saved privately. No refresh token is issued for client credentials.") & "</pre>"
%>
</body>
</html>