在 C++ 中建立授權原則存放區物件
授權原則存放區包含應用程式或應用程式群組安全策略的相關信息。 此資訊包括與市集相關聯的應用程式、作業、工作、使用者和使用者群組。 當使用 Authorization Manager 的應用程式初始化時,它會從存放區載入此資訊。 授權原則存放區必須位於受信任的系統上,因為該系統上的系統管理員具有高度的存放區存取權。
Authorization Manager 支援將授權原則儲存在 Active Directory 目錄服務或 XML 檔案中,如下列範例所示。 在 Authorization Manager API 中,授權原則存放區是由 AzAuthorizationStore 物件表示。 這些範例示範如何為 Active Directory 存放區和 XML 存放區建立 AzAuthorizationStore 物件。
建立 Active Directory 存放區
若要使用 Active Directory 來儲存授權原則,網域必須位於 Windows Server 2003 網域功能等級中。 授權原則存放區不能位於 非網域命名內容(也稱為應用程式分割區)。 建議您將存放區設置在 Program Data 容器中,該容器位於專門為授權政策存放區建立的新組織單位內。 也建議您將存放區放在與執行使用存放區之應用程式的應用程式伺服器位於相同的區域網路內。
下列範例示範如何建立 AzAuthorizationStore 物件,此物件代表 Active Directory 中的授權原則存放區。 此範例假設在名為 authmanager.com 的網域中有名為 Program Data 的現有 Active Directory 組織單位。
#pragma comment(lib, "duser.lib")
#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;
HRESULT hr;
void MyHandleError(char *s);
BSTR storeName = NULL;
// 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 a null VARIANT for function parameters.
VARIANT myVar;
VariantInit(&myVar);
// 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 in Active Directory. Use the
// AZ_AZSTORE_FLAG_CREATE flag.
hr = pStore->Initialize(AZ_AZSTORE_FLAG_CREATE, storeName, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize store.");
// Call the submit method to save changes to the new store.
hr = pStore->Submit(0, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not save data to the store.");
// Clean up resources.
pStore->Release();
VariantClear(&myVar);
SysFreeString(storeName);
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);
}
建立 SQL Server 存放區
授權管理員支援建立Microsoft SQL Server型授權原則存放區。 若要建立以 SQL Server 為基礎的授權存放區,請使用開頭為前置詞的 URL MSSQL://。 URL 必須包含有效的 SQL 連接字串、資料庫名稱和授權原則存放區的名稱:**MSSQL://ConnectionString/DatabaseName/**PolicyStoreName。
如果 SQL Server 的實例未包含指定的 Authorization Manager 資料庫,Authorization Manager 會使用該名稱建立新的資料庫。
注意
除非您明確為連線設定 SQL 加密,或設定使用因特網通訊協定安全性 (IPsec) 的網路流量加密,否則不會加密 SQL Server 存放區的連線。
下列範例示範如何建立 AzAuthorizationStore 物件,此物件代表 SQL Server 資料庫中的授權原則存放區。
#pragma comment(lib, "duser.lib")
#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;
HRESULT hr;
void MyHandleError(char *s);
BSTR storeName = NULL;
// 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.");
VARIANT myVar;
myVar.vt = VT_NULL;
// Allocate a string for the SQL Server store.
if(!(storeName = SysAllocString
(L"MSSQL://Driver={SQL Server};Server={AzServer};/AzDB/MyStore")))
MyHandleError("Could not allocate string.");
// Initialize the store. Use the
// AZ_AZSTORE_FLAG_CREATE flag.
hr = pStore->Initialize(AZ_AZSTORE_FLAG_CREATE, storeName, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize store.");
// Call the submit method to save changes to the new store.
hr = pStore->Submit(0, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not save data to the store.");
// Clean up resources.
pStore->Release();
SysFreeString(storeName);
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);
}
建立 XML 存放區
授權管理員支援以 XML 格式建立授權原則存放區。 XML 存放區可以位於應用程式執行所在的同一部計算機上,也可以從遠端儲存。 不支援直接編輯 XML 檔案。 使用 Authorization Manager MMC 嵌入式管理單元或 Authorization Manager API 來編輯原則存放區。
授權管理員不支援委派 XML 原則存放區的管理。 如需委派的相關信息,請參閱在 C++ 中委派許可權定義 。
下列範例示範如何建立代表 XML 檔案中授權原則存放區 AzAuthorizationStore 物件。
#pragma comment(lib, "duser.lib")
#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;
HRESULT hr;
void MyHandleError(char *s);
BSTR storeName = NULL;
// 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.");
VARIANT myVar;
myVar.vt = VT_NULL;
// Allocate a string for the distinguished name of the XML store.
if(!(storeName = SysAllocString(L"msxml://C:\\MyStore.xml")))
MyHandleError("Could not allocate string.");
// Initialize the store in an XML file. Use the
// AZ_AZSTORE_FLAG_CREATE flag.
hr = pStore->Initialize(AZ_AZSTORE_FLAG_CREATE, storeName, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not initialize store.");
// Call the submit method to save changes to the new store.
hr = pStore->Submit(0, myVar);
if (!(SUCCEEDED(hr)))
MyHandleError("Could not save data to the store.");
// Clean up resources.
pStore->Release();
SysFreeString(storeName);
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);
}