檢查角色成員資格

您可以呼叫 ISecurityCallContext::IsCallerInRole 方法,以判斷物件的直接呼叫端是否為特定角色的成員。 當您想要確保除非呼叫端是特定角色的成員,否則不會執行特定程式代碼區塊時,這項功能很有用。

例如,您可以使用 IsCallerInRole 來確保超過指定金額的交易,例如 $1000,只會由管理員角色的成員執行。 如果呼叫端不是管理員,且交易超過 $1000,則不會執行交易並顯示錯誤訊息。

存取IsCallerInRole的慣用方式是透過安全性呼叫內容對象,因為您可以使用與安全性呼叫內容物件相同的參考來取得安全性屬性。 不過,您也可以從 ObjectContext 物件存取 IsCallerInRole方法。 (請參閱 如需詳細資訊,ObjectContextIObjectContext 。)

如果您要開發 Microsoft Visual Basic 應用程式的元件,請呼叫 GetSecurityCallContext 函式,然後使用安全性呼叫內容來呼叫 IsCallerInRole,如下列範例所示:

If (GetSecurityCallContext.IsCallerInRole("Manager")) Then
   ' Go ahead and perform the transaction.
Else
   ' Display an error message.
End If

如果您要開發 C 或 C++ 應用程式,請使用 CoGetCallContext 來擷取 ISecurityCallContext 介面的指標。 然後,您會呼叫 ISecurityCallContext::IsCallerInRole,如下列範例所示:

ISecurityCallContext* pSecCtx;
VARIANT_BOOL bIsInRole;

HRESULT hr = CoGetCallContext(IID_ISecurityCallContext, (void**)&pSecCtx);
if (FAILED(hr)) throw(hr);
if (NULL == pSecCtx) { 
    // No security call context is available.
    // Display an error message and return.
    return E_FAIL;
}
hr = pSecCtx->IsCallerInRole(myRole, &bIsInRole);
return hr;

存取安全性呼叫內容資訊

判斷是否啟用角色型安全性

程序設計元件安全性