Azure Key Vault: Get a Token with Azure VM Managed Identity
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.
Run on an Azure VM with an enabled system-assigned identity, or set managed_identity_client_id for an attached user-assigned identity. The fixed link-local endpoint and Metadata header are specific to VM IMDS, not App Service. Proxying and redirects are disabled. Grant the identity appropriate vault data-plane permissions. Saves the same private token format as the other examples; does not print tokens. The example stops on IMDS errors; production IMDS retries must follow its service-specific guidance.
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
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
' Azure VM IMDS is a link-local HTTP endpoint. Do not proxy this request.
http.UseIEProxy = 0
http.ProxyDomain = ""
http.ConnectTimeout = 5
ERROR: ASP example cannot use request or response as a variable name.
request.AddHeader "Metadata","true"
request.AddParam "api-version","2018-02-01"
request.AddParam "resource","https://vault.azure.net"
If (config.HasMember("managed_identity_client_id") = 1) Then
success = stringCompare.SetString(config.StringOf("managed_identity_client_id"))
stringMatch1 = stringCompare.ContentsEqual("",1)
If ((config.JsonTypeOf("managed_identity_client_id") <> 1) Or (stringMatch1 = 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid managed_identity_client_id.") & "</pre>"
Response.End
End If
request.AddParam "client_id",config.StringOf("managed_identity_client_id")
End If
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("http://169.254.169.254/metadata/identity/oauth2/token",request,response)
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "IMDS request failed; run on an Azure VM with managed identity enabled.") & "</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"))
stringMatch2 = stringCompare.ContentsEqual("",1)
If ((result.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(result.StringOf("token_type"))
stringMatch3 = stringCompare.ContentsEqual("Bearer",1)
If (stringMatch3 <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Expected Bearer token.") & "</pre>"
Response.End
End If
If ((result.JsonTypeOf("expires_in") <> 2) And (result.JsonTypeOf("expires_in") <> 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Missing token lifetime.") & "</pre>"
Response.End
End If
set validate6 = Server.CreateObject("Chilkat.StringBuilder")
set alphabet6 = Server.CreateObject("Chilkat.StringBuilder")
success = validate6.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 = alphabet6.Append("0123456789")
If (success <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Could not initialize validation.") & "</pre>"
Response.End
End If
If ((validate6.Length < 1) Or (validate6.Length > 8)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid token lifetime length.") & "</pre>"
Response.End
End If
charIndex6 = 0
Do While (charIndex6 < validate6.Length)
If (alphabet6.Contains(validate6.GetRange(charIndex6,1,0),1) <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Invalid character in token lifetime.") & "</pre>"
Response.End
End If
charIndex6 = charIndex6 + 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>