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

Azure Storage Accounts: Obtain an ARM Token with Azure VM Managed Identity

See more Azure Storage Accounts Examples

Obtains an ARM token from Azure VM Instance Metadata Service (IMDS), without an application secret. Enable the VM's identity and assign it an Azure RBAC role covering the required storage-account management actions. Run this code inside that VM. App Service, Azure Arc, and other hosts have different identity endpoint contracts.

The fixed link-local endpoint uses HTTP by design. Do not proxy it, redirect it, or accept its address from user input. Set managedIdentityClientId to select a user-assigned identity attached to the VM, or leave it empty for a system-assigned identity. IMDS uses a resource parameter, not the v2 OAuth scope parameter.

The token is saved in private qa_data/tokens/azureToken.json. Rerun the IMDS request before the saved refresh_after_utc; do not use a refresh token. Protect the directory yourself and avoid printing the token. A VM hosting requirement does not mean this example needs a web server.

Microsoft API 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")
'  This example requires the Chilkat API to have been previously unlocked.
'  See Global.UnlockBundle for sample code.

set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.FollowRedirects = 0
http.ConnectTimeout = 30
http.ReadTimeout = 60
http.Accept = "application/json"
ERROR: ASP example cannot use request or response as a variable name.
set result = Server.CreateObject("Chilkat.JsonObject")
result.Strict = 1
'  Run on an Azure VM with managed identity enabled. This is not a public API.
'  IMDS deliberately uses a fixed link-local HTTP endpoint and must bypass proxies.
http.UseIEProxy = 0
http.ProxyDomain = ""
http.ConnectTimeout = 5
http.ReadTimeout = 15
set query = Server.CreateObject("Chilkat.JsonObject")
success = query.UpdateString("api-version","2018-02-01")
success = query.UpdateString("resource","https://management.azure.com/")
'  Empty selects the system-assigned identity. Set this to the CLIENT ID of an
'  identity assigned to the VM when selecting a user-assigned identity.
managedIdentityClientId = ""
success = stringCompare.SetString(managedIdentityClientId)
stringMatch1 = stringCompare.ContentsEqual("",1)
If (stringMatch1 <> 1) Then
    success = query.UpdateString("client_id",managedIdentityClientId)
End If

http.SetRequestHeader "Metadata","true"
set renewAt = Server.CreateObject("Chilkat.CkDateTime")
success = renewAt.SetFromCurrentSystemTime()
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not read the current time.") & "</pre>"
    Response.End
End If

success = http.HttpParams("GET","http://169.254.169.254/metadata/identity/oauth2/token",query,response)
If (response.StatusCode = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "No HTTP response. Inspect LastErrorText privately; it may contain credentials.") & "</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>"
    Response.Write "<pre>" & Server.HTMLEncode( "Retry-After: " & response.GetHeaderField("Retry-After")) & "</pre>"
    '  Inspect the error body privately. Do not log tokens, headers, or full responses.
    '  For 401 check the token/audience; for 403 check Azure RBAC and policy.
    '  Honor Retry-After for 429 and use bounded backoff for transient failures.
    Response.End
End If

If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "The HTTP response could not be read completely.") & "</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

If (result.JsonTypeOf("access_token") <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Missing or invalid access_token.") & "</pre>"
    Response.End
End If

If (result.StringOfEquals("access_token","",1) = 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "A nonempty access_token is required.") & "</pre>"
    Response.End
End If

If (result.StringOfEquals("token_type","Bearer",0) <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Expected a Bearer token.") & "</pre>"
    Response.End
End If

'  IMDS may return expires_in as a string. Validate before converting to int.
set lifetimeText = Server.CreateObject("Chilkat.StringBuilder")
set lifetimeCheck = Server.CreateObject("Chilkat.StringBuilder")
success = lifetimeText.SetString(result.StringOf("expires_in"))
lifetime = lifetimeText.IntValue
success = lifetimeCheck.AppendInt(lifetime)
If ((lifetimeText.ContentsEqual(lifetimeCheck.GetAsString(),1) <> 1) Or (lifetime <= 60) Or (lifetime > 604800)) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Invalid or unexpectedly short token lifetime.") & "</pre>"
    Response.End
End If

'  Start the lifetime at request start, not receipt, and renew at least a minute early.
success = renewAt.AddSeconds(lifetime - 60)
If (success <> 1) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Could not calculate token renewal time.") & "</pre>"
    Response.End
End If

set saved = Server.CreateObject("Chilkat.JsonObject")
success = saved.UpdateString("access_token",result.StringOf("access_token"))
success = saved.UpdateString("token_type","Bearer")
success = saved.UpdateInt("expires_in",lifetime)
success = saved.UpdateString("refresh_after_utc",renewAt.GetAsTimestamp(0))
'  Create qa_data/tokens beforehand with permissions limited to this application.
'  WriteFile does not create a secure directory or set operating-system ACLs.
success = saved.WriteFile("qa_data/tokens/azureToken.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( "Saved the ARM token without displaying it.") & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "Obtain a new token by UTC: " & saved.StringOf("refresh_after_utc")) & "</pre>"
'  A scheduler can rerun this token request before refresh_after_utc.
'  These app-only flows do not use an OAuth refresh_token.
'  The other account examples read this file and set http.AuthToken.

%>
</body>
</html>