检查角色成员身份

可以调用 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;

访问安全调用上下文信息

确定是否启用了基于角色的安全性

编程组件安全性