Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

Azure Storage: Authenticate with an Azure VM Managed Identity

See more Azure Cloud Storage Examples

Run on an Azure VM with a system-assigned or user-assigned managed identity. In storage.json supply account and optionally managed_client_id. Assign Storage Blob Data Reader (or a suitable data role) at the needed scope. The example obtains a Storage-audience token from IMDS and uses it for a Blob HEAD request. Do not send IMDS traffic through a proxy or make its destination user-controlled.

The token is saved to private qa_data/azure/storage-token.json with an expires_at timestamp for the related examples. Cache it until shortly before expiration, then repeat token acquisition. This flow does not use a refresh token. A 401 and a 403 need different investigation: token validity/audience versus data permissions or account policy. Allow time for RBAC changes to propagate.

Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.

Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.

Microsoft reference.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loTextCompare
LOCAL loConfig
LOCAL lnSameText1
LOCAL loIdentity
LOCAL loExpires
LOCAL lnSameText2
LOCAL lcTokenText
LOCAL loToken
LOCAL lnSameText3
LOCAL lnSameText4
LOCAL lnSeconds
LOCAL lcAccessToken
LOCAL loEndpoint
LOCAL lcResourcePath
LOCAL loRest
LOCAL loConnection
LOCAL lcResponseText
LOCAL loResult
LOCAL loBearer

lnSuccess = 0

*  StringBuilder compares text by value in every generated language.
loTextCompare = CreateObject('Chilkat.StringBuilder')
*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("qa_data/azure/storage.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private Azure configuration."
    RELEASE loTextCompare
    RELEASE loConfig
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loConfig.StringOf("account"))
lnSameText1 = loTextCompare.ContentsEqual("",1)
IF ((loConfig.JsonTypeOf("account") <> 1) OR (lnSameText1 = 1)) THEN
    ? "Supply the storage account name."
    RELEASE loTextCompare
    RELEASE loConfig
    CANCEL
ENDIF

loIdentity = CreateObject('Chilkat.Rest')
loIdentity.ConnectTimeoutMs = 20000
loIdentity.IdleTimeoutMs = 60000
loExpires = CreateObject('Chilkat.CkDateTime')
lnSuccess = loExpires.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

*  Azure VM IMDS only: fixed link-local endpoint, direct connection, no proxy.
*  App Service, Functions and Azure Arc have different identity endpoints.
lnSuccess = loIdentity.Connect("169.254.169.254",80,0,0)
IF (lnSuccess <> 1) THEN
    ? "IMDS is available only on an eligible Azure VM."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

lnSuccess = loIdentity.AddHeader("Metadata","true")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

lnSuccess = loIdentity.AddQueryParam("api-version","2018-02-01")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

lnSuccess = loIdentity.AddQueryParam("resource","https://storage.azure.com/")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

*  Optional user-assigned identity. Omit for the VM's system-assigned identity.
IF (loConfig.HasMember("managed_client_id") = 1) THEN
    loTextCompare.Clear()
    loTextCompare.SetString(loConfig.StringOf("managed_client_id"))
    lnSameText2 = loTextCompare.ContentsEqual("",1)
    IF ((loConfig.JsonTypeOf("managed_client_id") <> 1) OR (lnSameText2 = 1)) THEN
        ? "Invalid managed_client_id."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loIdentity
        RELEASE loExpires
        CANCEL
    ENDIF

    lnSuccess = loIdentity.AddQueryParam("client_id",loConfig.StringOf("managed_client_id"))
    IF (lnSuccess <> 1) THEN
        ? "Could not prepare the request."
        RELEASE loTextCompare
        RELEASE loConfig
        RELEASE loIdentity
        RELEASE loExpires
        CANCEL
    ENDIF

ENDIF

lcTokenText = loIdentity.FullRequestNoBody("GET","/metadata/identity/oauth2/token")
IF (loIdentity.LastMethodSuccess <> 1) THEN
    ? "Token request did not complete; inspect diagnostics privately."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

IF (loIdentity.ResponseStatusCode <> 200) THEN
    ? "Token request failed; inspect the status privately, never print the response token."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    CANCEL
ENDIF

loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.Load(lcTokenText)
IF (lnSuccess <> 1) THEN
    ? "Expected token JSON."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    CANCEL
ENDIF

loTextCompare.Clear()
loTextCompare.SetString(loToken.StringOf("access_token"))
lnSameText3 = loTextCompare.ContentsEqual("",1)
loTextCompare.Clear()
loTextCompare.SetString(loToken.StringOf("token_type"))
lnSameText4 = loTextCompare.ContentsEqual("Bearer",1)
IF ((loToken.JsonTypeOf("access_token") <> 1) OR (lnSameText3 = 1) OR (lnSameText4 <> 1)) THEN
    ? "Missing bearer token."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    CANCEL
ENDIF

lnSeconds = loToken.IntOf("expires_in")
IF ((lnSeconds < 120) OR (lnSeconds > 86400)) THEN
    ? "Unexpected token lifetime."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    CANCEL
ENDIF

lnSuccess = loExpires.AddSeconds(lnSeconds)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    CANCEL
ENDIF

lnSuccess = loToken.UpdateString("expires_at",loExpires.GetAsTimestamp(0))
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    CANCEL
ENDIF

lnSuccess = loToken.WriteFile("qa_data/azure/storage-token.json")
IF (lnSuccess <> 1) THEN
    ? "Could not persist token privately."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    CANCEL
ENDIF

lcAccessToken = loToken.StringOf("access_token")
loEndpoint = CreateObject('Chilkat.StringBuilder')
lnSuccess = loEndpoint.Append(loConfig.StringOf("account"))
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    CANCEL
ENDIF

lnSuccess = loEndpoint.Append(".blob.core.windows.net")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    CANCEL
ENDIF

*  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
*  Keep slash separators; names and paths are case-sensitive.
lcResourcePath = "/examplecontainer/example.bin"
loRest = CreateObject('Chilkat.Rest')
loRest.ConnectTimeoutMs = 20000
loRest.IdleTimeoutMs = 60000
loConnection = CreateObject('Chilkat.Socket')
loConnection.RequireSslCertVerify = 1
loConnection.MaxReadIdleMs = 60000
loConnection.MaxSendIdleMs = 60000
lnSuccess = loConnection.Connect(loEndpoint.GetAsString(),443,1,20000)
IF (lnSuccess <> 1) THEN
    ? "TLS connection failed; inspect diagnostics privately."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

*  Do not replay a write automatically after losing its response.
lnSuccess = loRest.UseConnection(loConnection,0)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    CANCEL
ENDIF

loResult = CreateObject('Chilkat.Xml')
loResult.Strict = 1
lnSuccess = loRest.AddHeader("x-ms-version","2026-04-06")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loResult
    CANCEL
ENDIF

loBearer = CreateObject('Chilkat.StringBuilder')
lnSuccess = loBearer.Append("Bearer ")
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loResult
    RELEASE loBearer
    CANCEL
ENDIF

lnSuccess = loBearer.Append(lcAccessToken)
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loResult
    RELEASE loBearer
    CANCEL
ENDIF

lnSuccess = loRest.AddHeader("Authorization",loBearer.GetAsString())
IF (lnSuccess <> 1) THEN
    ? "Could not prepare the request."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loResult
    RELEASE loBearer
    CANCEL
ENDIF

lcResponseText = loRest.FullRequestNoBody("HEAD",lcResourcePath)
IF (loRest.LastMethodSuccess <> 1) THEN
    ? "Request did not complete; outcome may be uncertain."
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loResult
    RELEASE loBearer
    CANCEL
ENDIF

IF (loRest.ResponseStatusCode <> 200) THEN
    ? "HTTP status: " + STR(loRest.ResponseStatusCode)
    ? "Azure error code: " + loRest.ResponseHdrByName("x-ms-error-code")
    ? "Request ID: " + loRest.ResponseHdrByName("x-ms-request-id")
    *  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    RELEASE loTextCompare
    RELEASE loConfig
    RELEASE loIdentity
    RELEASE loExpires
    RELEASE loToken
    RELEASE loEndpoint
    RELEASE loRest
    RELEASE loConnection
    RELEASE loResult
    RELEASE loBearer
    CANCEL
ENDIF

? "Storage token obtained and blob properties read."
? "ETag: " + loRest.ResponseHdrByName("ETag")

RELEASE loTextCompare
RELEASE loConfig
RELEASE loIdentity
RELEASE loExpires
RELEASE loToken
RELEASE loEndpoint
RELEASE loRest
RELEASE loConnection
RELEASE loResult
RELEASE loBearer