Sample code for 30+ languages & platforms
Unicode C

Configure Azure SAS Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthAzureSas, which associates an AuthAzureSAS provider so requests are authorized with an Azure Shared Access Signature token.

Background. A SAS token grants time-limited, permission-scoped access to Azure Storage resources without exposing the account key on the wire. Use SAS authorization when a query-token approach is preferred over Shared Key headers.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkAuthAzureSASW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    BOOL bAutoReconnect;
    HCkAuthAzureSASW azSas;
    const wchar_t *responseText;

    success = FALSE;

    rest = CkRestW_Create();
    bTls = TRUE;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"example.com",443,bTls,bAutoReconnect);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        return;
    }

    //  Configure Azure Shared Access Signature (SAS) authentication.  Provide the account access key
    //  and the SAS token query parameters; Chilkat adds the SAS token to each request.
    azSas = CkAuthAzureSASW_Create();
    //  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    //  interactive prompt, environment variable, or a secrets vault.
    CkAuthAzureSASW_putAccessKey(azSas,L"BASE64_ACCOUNT_ACCESS_KEY");

    //  Associate the provider with the REST object.
    success = CkRestW_SetAuthAzureSas(rest,azSas);
    if (success == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAzureSASW_Dispose(azSas);
        return;
    }

    responseText = CkRestW_fullRequestNoBody(rest,L"GET",L"/mycontainer/myblob.txt");
    if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAzureSASW_Dispose(azSas);
        return;
    }

    wprintf(L"%s\n",responseText);


    CkRestW_Dispose(rest);
    CkAuthAzureSASW_Dispose(azSas);

    }