Establishing a Client Context with Authorization Manager in C++

In Authorization Manager, an application determines whether a client is given access to an operation by calling the AccessCheck method of an IAzClientContext object, which represents a client context.

An application can create a client context with a handle to a token, a domain and user name, or a string representation of the security identifier (SID) of the client.

Use the InitializeClientContextFromToken, InitializeClientContextFromName, and InitializeClientContextFromStringSid methods of the IAzApplication interface to create a client context.

The following example shows how to create an IAzClientContext object from a client token. The example assumes that there is an existing XML policy store named MyStore.xml in the root directory of drive C, that this store contains an application named Expense, and that the variable hToken contains a valid client token.

#include <windows.h>

void ExpenseCheck(ULONGLONG hToken)
    IAzAuthorizationStore* pStore = NULL;
    IAzApplication* pApp = NULL;
    IAzClientContext* pClientContext = NULL;
    BSTR storeName = NULL;
    BSTR appName = NULL;
    HRESULT hr;
    void MyHandleError(char *s);
 //  Create a null VARIANT for function parameters.
    VARIANT myVar;

    //  Initialize COM.
    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not initialize COM.");

    //  Create the AzAuthorizationStore object.
    hr = CoCreateInstance(
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not create AzAuthorizationStore object.");

    //  Allocate a string for the policy store.
    if(!(storeName = SysAllocString(L"msxml://c:\\MyStore.xml")))
        MyHandleError("Could not allocate string.");
    //  Initialize the store.
    hr = pStore->Initialize(0, storeName, myVar);
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not initialize store.");

    //  Create an application object.
    if (!(appName = SysAllocString(L"Expense")))
        MyHandleError("Could not allocate application name string.");
    hr = pStore->OpenApplication(appName, myVar, &pApp);
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not open application.");

    //  Create a client context from a token handle.
    hr = pApp->InitializeClientContextFromToken(hToken, myVar,
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not create client context.");

    //  Use the client context as needed.

    //  Clean up resources.

void MyHandleError(char *s)
    printf("An error occurred in running the program.\n");
    printf("Error number %x\n.",GetLastError());
    printf("Program terminating.\n");