Sample code for 30+ languages & platforms
.NET Core C# Requires Chilkat v11.6.0+

Azure Table: Read an Entity with Azure VM Managed Identity

See more Azure Table Service Examples

Run on an Azure VM with a system-assigned identity, or supply managed_client_id for an attached user-assigned identity. The fixed IMDS endpoint is specific to VMs; App Service and Azure Arc use different flows. Assign Storage Table Data Reader or an appropriate Table data role. A management-plane Contributor role alone is not sufficient. Acquires a Storage-audience token and actually uses it to retrieve an entity, without an account key.

Saves only access_token, expires_at and resource to private table-token.json, replacing the prior cache. Uses request-start time for expiry. Reacquire before expiry; do not print the token or embed a client secret in a public client. This sample stops on identity errors rather than applying a generic retry policy.

Targets Azure Storage Table service in the public cloud, not Azure Cosmos DB for Table. Uses Chilkat 11.6.0 or later and an unlocked library. Direct data requests pin x-ms-version 2026-04-06 and OData 3.0. Shared Key examples read private qa_data/azure/storage.json with account and access_key string fields. Authentication examples explain their additional fields. Create private local directories before running; keep credentials, tokens, SAS, entity values, checkpoints and diagnostics outside public content and source control.

HTTPS certificates are verified. Transport completion and HTTP status are checked separately. Writes are not automatically retried after an uncertain outcome. Entity keys, ETags and continuation tokens remain strings. Examples operate on the existing ExampleCustomers table unless noted; review the targets before adapting them. Microsoft Entra ID with suitable Table data roles is preferred where available.

Microsoft reference.

Chilkat .NET Core C# Downloads

.NET Core C#
bool success = false;

//  StringBuilder compares text by value in every generated language.
Chilkat.StringBuilder textCompare = new Chilkat.StringBuilder();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/storage.json");
if (success != true) {
    Debug.WriteLine("Could not load private Azure configuration.");
    return;
}

textCompare.Clear();
textCompare.SetString(config.StringOf("account"));
bool sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
    Debug.WriteLine("Supply the storage account name.");
    return;
}

Chilkat.Rest identity = new Chilkat.Rest();
identity.ConnectTimeoutMs = 5000;
identity.IdleTimeoutMs = 30000;
Chilkat.CkDateTime expires = new Chilkat.CkDateTime();
success = expires.SetFromCurrentSystemTime();
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

//  Fixed Azure VM IMDS endpoint; no proxy, no redirects, no automatic reconnect.
success = identity.Connect("169.254.169.254",80,false,false);
if (success != true) {
    Debug.WriteLine("IMDS requires an Azure VM with managed identity enabled.");
    return;
}

success = identity.AddHeader("Metadata","true");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = identity.AddQueryParam("api-version","2018-02-01");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = identity.AddQueryParam("resource","https://storage.azure.com/");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if (config.HasMember("managed_client_id") == true) {
    textCompare.Clear();
    textCompare.SetString(config.StringOf("managed_client_id"));
    bool sameText2 = textCompare.ContentsEqual("",true);
    if ((config.JsonTypeOf("managed_client_id") != 1) || (sameText2 == true)) {
        Debug.WriteLine("Missing string managed_client_id.");
        return;
    }

    success = identity.AddQueryParam("client_id",config.StringOf("managed_client_id"));
    if (success != true) {
        Debug.WriteLine("Could not prepare the request.");
        return;
    }

}

string tokenText = identity.FullRequestNoBody("GET","/metadata/identity/oauth2/token");
if (identity.LastMethodSuccess != true) {
    Debug.WriteLine("Token request did not complete.");
    return;
}

if (identity.ResponseStatusCode != 200) {
    Debug.WriteLine("Token request failed; keep its body and diagnostics private.");
    return;
}

Chilkat.JsonObject token = new Chilkat.JsonObject();
token.Strict = true;
success = token.Load(tokenText);
if (success != true) {
    Debug.WriteLine("Expected token JSON.");
    return;
}

textCompare.Clear();
textCompare.SetString(token.StringOf("access_token"));
bool sameText3 = textCompare.ContentsEqual("",true);
if ((token.JsonTypeOf("access_token") != 1) || (sameText3 == true)) {
    Debug.WriteLine("Missing string access_token.");
    return;
}

textCompare.Clear();
textCompare.SetString(token.StringOf("token_type"));
bool sameText4 = textCompare.ContentsEqual("Bearer",true);
if (sameText4 != true) {
    Debug.WriteLine("Expected a bearer token.");
    return;
}

if ((token.JsonTypeOf("expires_in") != 2) && (token.JsonTypeOf("expires_in") != 1)) {
    Debug.WriteLine("Invalid token lifetime type.");
    return;
}

Chilkat.StringBuilder validate3 = new Chilkat.StringBuilder();
Chilkat.StringBuilder alphabet3 = new Chilkat.StringBuilder();
success = validate3.Append(token.StringOf("expires_in"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = alphabet3.Append("0123456789");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if ((validate3.Length < 1) || (validate3.Length > 6)) {
    Debug.WriteLine("Invalid token lifetime length.");
    return;
}

int index3 = 0;
while ((index3 < validate3.Length)) {
    if (alphabet3.Contains(validate3.GetRange(index3,1,false),true) != true) {
        Debug.WriteLine("Invalid token lifetime.");
        return;
    }

    index3 = index3 + 1;
}

int seconds = token.IntOf("expires_in");
if ((seconds < 120) || (seconds > 86400)) {
    Debug.WriteLine("Unexpected token lifetime.");
    return;
}

success = expires.AddSeconds(seconds);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.CkDateTime now = new Chilkat.CkDateTime();
success = now.SetFromCurrentSystemTime();
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if (expires.DiffSeconds(now) <= 60) {
    Debug.WriteLine("Token expired while the request was in progress.");
    return;
}

Chilkat.JsonObject saved = new Chilkat.JsonObject();
success = saved.UpdateString("access_token",token.StringOf("access_token"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = saved.UpdateString("expires_at",expires.GetAsTimestamp(false));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = saved.UpdateString("resource","https://storage.azure.com/");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = saved.WriteFile("qa_data/azure/table-token.json");
if (success != true) {
    Debug.WriteLine("Could not save the private token file.");
    return;
}

Chilkat.StringBuilder accountText = new Chilkat.StringBuilder();
success = accountText.Append(config.StringOf("account"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if ((accountText.Length < 3) || (accountText.Length > 24)) {
    Debug.WriteLine("Storage account names need 3-24 lowercase letters or digits.");
    return;
}

Chilkat.StringBuilder allowedAccountChars = new Chilkat.StringBuilder();
success = allowedAccountChars.Append("abcdefghijklmnopqrstuvwxyz0123456789");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

int charIndex = 0;
while ((charIndex < accountText.Length)) {
    if (allowedAccountChars.Contains(accountText.GetRange(charIndex,1,false),true) != true) {
        Debug.WriteLine("Storage account names need lowercase letters or digits.");
        return;
    }

    charIndex = charIndex + 1;
}

Chilkat.StringBuilder endpoint = new Chilkat.StringBuilder();
success = endpoint.Append(config.StringOf("account"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = endpoint.Append(".table.core.windows.net");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

//  Table names contain only letters and digits and are case-insensitive.
//  Entity PartitionKey and RowKey values are case-sensitive.
string resourcePath = "/ExampleCustomers()";
Chilkat.Rest rest = new Chilkat.Rest();
rest.ConnectTimeoutMs = 20000;
rest.IdleTimeoutMs = 60000;
Chilkat.Socket connection = new Chilkat.Socket();
connection.RequireSslCertVerify = true;
connection.MaxReadIdleMs = 60000;
connection.MaxSendIdleMs = 60000;
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS connection failed; inspect diagnostics privately.");
    return;
}

//  Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

string responseText;
Chilkat.JsonObject result = new Chilkat.JsonObject();
result.Strict = true;
success = rest.AddHeader("DataServiceVersion","3.0;NetFx");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("MaxDataServiceVersion","3.0;NetFx");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Content-Type","application/json");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Accept","application/json;odata=minimalmetadata");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("x-ms-version","2026-04-06");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.StringBuilder authorization = new Chilkat.StringBuilder();
success = authorization.Append("Bearer ");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = authorization.Append(token.StringOf("access_token"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Authorization",authorization.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

string partitionKey = "customers-west";
string rowKey = "customer-001";
Chilkat.StringBuilder encodedPk = new Chilkat.StringBuilder();
success = encodedPk.Append(partitionKey);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

int escapedencodedPk = encodedPk.Replace("'","''");
success = encodedPk.Encode("url_rfc3986","utf-8");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

Chilkat.StringBuilder encodedRk = new Chilkat.StringBuilder();
success = encodedRk.Append(rowKey);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

int escapedencodedRk = encodedRk.Replace("'","''");
success = encodedRk.Encode("url_rfc3986","utf-8");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

//  Escape apostrophes for OData first, then percent-encode each key once.
Chilkat.StringBuilder entityPath = new Chilkat.StringBuilder();
success = entityPath.Append("/ExampleCustomers(PartitionKey='");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = entityPath.Append(encodedPk.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = entityPath.Append("',RowKey='");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = entityPath.Append(encodedRk.GetAsString());
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = entityPath.Append("')");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

resourcePath = entityPath.GetAsString();
success = now.SetFromCurrentSystemTime();
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

if (expires.DiffSeconds(now) <= 60) {
    Debug.WriteLine("Reacquire a token before the Table request.");
    return;
}

responseText = rest.FullRequestNoBody("GET",resourcePath);
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Request did not complete; inspect the target before retrying a write.");
    return;
}

if (rest.ResponseStatusCode != 200) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
    Debug.WriteLine("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
    //  404 is missing, 409 is conflict, and 412 is a stale ETag: none are success.
    //  Keep response bodies private. Reconcile uncertain writes before retrying.
    return;
}

success = result.Load(responseText);
if (success != true) {
    Debug.WriteLine("Expected well-formed JSON.");
    return;
}

textCompare.Clear();
textCompare.SetString(result.StringOf("PartitionKey"));
bool sameText5 = textCompare.ContentsEqual("",true);
if ((result.JsonTypeOf("PartitionKey") != 1) || (sameText5 == true)) {
    Debug.WriteLine("Missing string PartitionKey.");
    return;
}

textCompare.Clear();
textCompare.SetString(result.StringOf("RowKey"));
bool sameText6 = textCompare.ContentsEqual("",true);
if ((result.JsonTypeOf("RowKey") != 1) || (sameText6 == true)) {
    Debug.WriteLine("Missing string RowKey.");
    return;
}

textCompare.Clear();
textCompare.SetString(result.StringOf("PartitionKey"));
bool sameText7 = textCompare.ContentsEqual(partitionKey,true);
textCompare.Clear();
textCompare.SetString(result.StringOf("RowKey"));
bool sameText8 = textCompare.ContentsEqual(rowKey,true);
if ((sameText7 != true) || (sameText8 != true)) {
    Debug.WriteLine("Returned entity keys do not match.");
    return;
}

Debug.WriteLine("Storage token obtained and Table entity retrieved.");