在 C++ 中委托权限定义

存储在 Active Directory 中的授权策略存储支持管理委派。 可以在应用商店、应用程序或范围级别将管理委托给用户和组。

每个级别都有一个管理员和读者列表。 存储区、应用程序或范围的管理员可以在委托级别读取和修改策略存储。 读者可以在委托级别读取策略存储,但不能修改该存储区。

作为应用程序的管理员或读者的用户或组还必须添加为包含该应用程序的策略存储的委托用户。 同样,必须将作为作用域管理员或读取者的用户或组添加为包含该作用域的应用程序的委托用户。

例如,若要委托某个范围的管理,请先通过调用 IAzAuthorizationStore::AddDelegatedPolicyUser 方法将用户或组添加到包含该范围的存储的委托用户列表中。 然后,通过调用 IAzApplication::AddDelegatedPolicyUser 方法,将用户或组添加到包含作用域的应用程序的委托用户列表中。 最后,通过调用 IAzScope::AddPolicyAdministrator 方法,将用户或组添加到范围的管理员列表中。

基于 XML 的策略存储不支持任何级别的委派。

如果作用域包含包含授权规则的任务定义或包含授权规则的角色定义,则无法委托存储在 Active Directory 中的授权存储中的范围。

以下示例演示如何委托应用程序的管理。 该示例假定指定位置存在现有的 Active Directory 授权策略存储区,此策略存储区包含名为 Expense 的应用程序,并且此应用程序不包含具有业务规则脚本的任务。

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0502
#endif

#include <windows.h>
#include <stdio.h>
#include <azroles.h>
#include <objbase.h>

void main(void)
{
    IAzAuthorizationStore* pStore = NULL;
    IAzApplication* pApp = NULL;
    HRESULT hr;
    void MyHandleError(char *s);
    BSTR storeName = NULL;
    BSTR appName = NULL;
    BSTR userName = NULL;
    VARIANT myVar;
    
    //  Initialize COM.
    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not initialize COM.");

    //  Create the AzAuthorizationStore object.
    hr = CoCreateInstance(
   /*"b2bcff59-a757-4b0b-a1bc-ea69981da69e"*/
         __uuidof(AzAuthorizationStore),
         NULL,
         CLSCTX_ALL,
   /*"edbd9ca9-9b82-4f6a-9e8b-98301e450f14"*/
         __uuidof(IAzAuthorizationStore),
         (void**)&pStore);
    if (!(SUCCEEDED(hr)))
        MyHandleError("Could not create AzAuthorizationStore object.");
    
    //  Create null VARIANT for parameters.
    myVar.vt = VT_NULL;

    //  Allocate a string for the distinguished name of the
 //  Active Directory store.
    if(!(storeName = SysAllocString
   (L"msldap://CN=MyAzStore,CN=Program Data,DC=authmanager,DC=com")))
        MyHandleError("Could not allocate string.");
    
    //  Initialize the store.
    hr = pStore->Initialize
   (AZ_AZSTORE_FLAG_MANAGE_STORE_ONLY, 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.");

    //  Add a delegated policy user to the store.
    if (!(userName = SysAllocString(L"ExampleDomain\\UserName")))
        MyHandleError("Could not allocate username string.");
    hr = pStore->AddDelegatedPolicyUserName(userName, myVar);
    if (!(SUCCEEDED(hr)))
        MyHandleError
   ("Could not add user to store as delegated policy user.");

    //  Add the user as an administrator of the application.
    hr = pApp->AddPolicyAdministratorName(userName, myVar);
    if (!(SUCCEEDED(hr)))
        MyHandleError
   ("Could not add user to application as administrator.");

    

    //  Clean up resources.
    pStore->Release();
    pApp->Release();
    SysFreeString(storeName);
    SysFreeString(appName);
    SysFreeString(userName);
    CoUninitialize();
}

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