Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.6.0+

Azure Storage Accounts: Configure Storage Firewall IP and Subnet Rules

See more Azure Storage Accounts Examples

Reads and preserves existing network ACLs, adds an IP rule and optionally a subnet rule, then prepares publicNetworkAccess=Enabled with defaultAction=Deny. Replace the documentation IP with a real public outbound IPv4 address. A subnet must already have the appropriate Storage service endpoint.

Review the target and set applyChanges=true to submit. This can interrupt clients and can reopen a disabled public endpoint for selected sources. Existing bypass exceptions and resource-instance rules remain in effect. These rules do not create private endpoints, configure DNS, or grant data permissions. Coordinate concurrent network-policy writers.

See Storage firewall and network access for same-region Azure traffic, service endpoints, private networking, and service-specific limitations.

These calls manage Azure resources at management.azure.com. They do not upload or download stored data. Supply a current Microsoft Entra access token for Azure Resource Manager in private qa_data/tokens/azureToken.json, with a nonempty access_token string. For v2 client credentials, request https://management.azure.com/.default; obtain a new token when it expires. A storage account key, SAS, Microsoft Graph token, or Storage data-plane token is not an ARM token. Never print the token or commit its file.

Azure RBAC must grant the caller the required management actions at the appropriate scope. An app registration alone does not grant resource access. Management permissions and blob/file data permissions are distinct. These examples target public Azure; sovereign clouds require the corresponding authority, ARM endpoint, token audience, and continuation-host validation.

Microsoft API reference

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpResponseW.h>
#include <CkUrlW.h>
#include <CkJsonArrayW.h>

void ChilkatSample(void)
    {
    bool success = false;

    CkStringBuilderW stringCompare;
    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global.UnlockBundle for sample code.

    //  This is Azure Resource Manager (the management plane), not a blob data request.
    //  Load an unexpired Microsoft Entra token whose audience is Azure Resource Manager.
    //  For the v2 token endpoint, request scope https://management.azure.com/.default.
    //  Keep the token file private and out of source control and public web directories.
    CkJsonObjectW token;
    token.put_Strict(true);
    success = token.LoadFile(L"qa_data/tokens/azureToken.json");
    if (success != true) {
        wprintf(L"Could not load the private Azure Resource Manager token file.\n");
        return;
    }

    if ((token.JsonTypeOf(L"access_token") != 1) || (token.StringOfEquals(L"access_token",L"",true) == true)) {
        wprintf(L"A nonempty access_token string is required.\n");
        return;
    }

    //  Use your subscription ID as a string. The all-zero value is only a placeholder.
    const wchar_t *subscriptionId = L"00000000-0000-0000-0000-000000000000";
    CkHttpW http;
    http.put_AuthToken(token.stringOf(L"access_token"));
    http.put_Accept(L"application/json");
    http.put_RequireSslCertVerify(true);
    http.put_FollowRedirects(false);
    http.put_ConnectTimeout(30);
    http.put_ReadTimeout(60);
    success = http.SetUrlVar(L"subscriptionId",subscriptionId);
    if (success != true) {
        wprintf(L"Could not encode subscription ID.\n");
        return;
    }

    CkHttpResponseW response;
    CkJsonObjectW result;
    result.put_Strict(true);
    CkUrlW parsed;
    const wchar_t *resourceGroupName = L"my-resource-group";
    success = http.SetUrlVar(L"resourceGroupName",resourceGroupName);
    if (success != true) {
        wprintf(L"Could not encode resource group.\n");
        return;
    }

    const wchar_t *accountName = L"youruniquestorageaccount";
    success = http.SetUrlVar(L"accountName",accountName);
    if (success != true) {
        wprintf(L"Could not encode account name.\n");
        return;
    }

    const wchar_t *resourceUrl = L"https://management.azure.com/subscriptions/{$subscriptionId}/resourceGroups/{$resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{$accountName}?api-version=2026-09-01";
    success = http.HttpNoBody(L"GET",resourceUrl,response);
    if (response.get_StatusCode() == 0) {
        wprintf(L"No HTTP response. Inspect LastErrorText privately; it may contain credentials.\n");
        return;
    }

    if ((response.get_StatusCode() != 200)) {
        wprintf(L"HTTP status: %d\n",response.get_StatusCode());
        wprintf(L"Request ID: %s\n",response.getHeaderField(L"x-ms-request-id"));
        wprintf(L"Retry-After: %s\n",response.getHeaderField(L"Retry-After"));
        //  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.
        return;
    }

    if (success != true) {
        wprintf(L"The HTTP response could not be read completely.\n");
        return;
    }

    success = result.Load(response.bodyStr());
    if (success != true) {
        wprintf(L"Expected a well-formed JSON response.\n");
        return;
    }

    if (result.JsonTypeOf(L"properties.networkAcls") != 3) {
        wprintf(L"Missing or invalid properties.networkAcls.\n");
        return;
    }

    CkJsonObjectW network;
    success = result.ObjectOf2(L"properties.networkAcls",network);
    if (success != true) {
        wprintf(L"Could not read network rules.\n");
        return;
    }

    //  Preserve existing exceptions, resource-instance rules, IP rules, and subnet IDs.
    //  Replace this documentation address with your client's PUBLIC outbound IPv4.
    const wchar_t *allowedIp = L"198.51.100.10";
    //  Optional: full ARM subnet ID, kept as a string. Its Storage service endpoint
    //  must already be enabled. This example does not create a subnet or endpoint.
    const wchar_t *subnetId = L"";
    CkJsonArrayW ipRules;
    if (network.HasMember(L"ipRules") != true) {
        network.UpdateNewArray(L"ipRules");
    }

    success = network.ArrayOf2(L"ipRules",ipRules);
    if (success != true) {
        wprintf(L"Invalid IP rule array.\n");
        return;
    }

    CkJsonObjectW rule;
    bool ipFound = false;
    int i = 0;
    while ((i < ipRules.get_Size())) {
        success = ipRules.ObjectAt2(i,rule);
        if (success != true) {
            wprintf(L"Invalid IP rule.\n");
            return;
        }

        if (rule.StringOfEquals(L"value",allowedIp,true) == true) {
            ipFound = true;
        }

        i = i + 1;
    }

    if (ipFound != true) {
        success = ipRules.AddObjectAt2(-1,rule);
        if (success != true) {
            wprintf(L"Could not append IP rule.\n");
            return;
        }

        rule.UpdateString(L"value",allowedIp);
        rule.UpdateString(L"action",L"Allow");
    }

    CkJsonArrayW subnets;
    if (network.HasMember(L"virtualNetworkRules") != true) {
        network.UpdateNewArray(L"virtualNetworkRules");
    }

    success = network.ArrayOf2(L"virtualNetworkRules",subnets);
    if (success != true) {
        wprintf(L"Invalid subnet rule array.\n");
        return;
    }

    bool subnetFound = false;
    i = 0;
    while ((i < subnets.get_Size())) {
        success = subnets.ObjectAt2(i,rule);
        if (success != true) {
            wprintf(L"Invalid subnet rule.\n");
            return;
        }

        //  state is returned by GET but is not a writable rule property.
        rule.Delete(L"state");
        if (rule.StringOfEquals(L"id",subnetId,false) == true) {
            subnetFound = true;
        }

        i = i + 1;
    }

    stringCompare.SetString(subnetId);
    bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
    if ((stringMatch1 != true) && (subnetFound != true)) {
        success = subnets.AddObjectAt2(-1,rule);
        if (success != true) {
            wprintf(L"Could not append subnet rule.\n");
            return;
        }

        rule.UpdateString(L"id",subnetId);
        rule.UpdateString(L"action",L"Allow");
    }

    network.UpdateString(L"defaultAction",L"Deny");
    CkJsonObjectW request;
    CkJsonObjectW properties;
    success = request.AppendObject2(L"properties",properties);
    if (success != true) {
        wprintf(L"Could not create properties.\n");
        return;
    }

    success = properties.AppendObjectCopy(L"networkAcls",network);
    if (success != true) {
        wprintf(L"Could not copy network rules.\n");
        return;
    }

    //  Enabled + defaultAction Deny means selected public networks. Disabled would
    //  block public endpoint access; SecuredByPerimeter is a different policy mode.
    properties.UpdateString(L"publicNetworkAccess",L"Enabled");
    bool applyChanges = false;
    if (applyChanges != true) {
        wprintf(L"Network changes prepared, not applied. Review rules and set applyChanges to true.\n");
        return;
    }

    //  This may reopen a previously disabled public endpoint for the selected sources.
    //  Preserve exceptions intentionally; preserved trusted-service bypasses still apply.
    //  On a write timeout, read the resource before deciding whether to retry.
    success = http.HttpJson(L"PATCH",resourceUrl,request,L"application/json",response);
    if (response.get_StatusCode() == 0) {
        wprintf(L"No HTTP response. Inspect LastErrorText privately; it may contain credentials.\n");
        return;
    }

    if ((response.get_StatusCode() != 200)) {
        wprintf(L"HTTP status: %d\n",response.get_StatusCode());
        wprintf(L"Request ID: %s\n",response.getHeaderField(L"x-ms-request-id"));
        wprintf(L"Retry-After: %s\n",response.getHeaderField(L"Retry-After"));
        //  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.
        return;
    }

    if (success != true) {
        wprintf(L"The HTTP response could not be read completely.\n");
        return;
    }

    success = result.Load(response.bodyStr());
    if (success != true) {
        wprintf(L"Expected a well-formed JSON response.\n");
        return;
    }

    wprintf(L"Network policy update returned successfully. Verify access from intended clients.\n");
    }