Determining Whether Role-Based Security Is Enabled

By using the ISecurityCallContext::IsSecurityEnabled method available from the security call context object, you can determine whether security is enabled for the current object. You should call IsSecurityEnabled before you use ISecurityCallContext::IsCallerInRole to check role membership because IsCallerInRole returns True if security is not enabled.

Microsoft Visual Basic developers call GetSecurityCallContext to obtain a reference to a SecurityCallContext object and then call IsSecurityEnabled, as shown in the following example:

Dim objSecCallCtx As SecurityCallContext
Dim boolSecEn As Boolean
Set objSecCallCtx = GetSecurityCallContext()
boolSecEn = objSecCallCtx.IsSecurityEnabled()
 

Microsoft Visual C++ developers can call ISecurityCallContext::IsSecurityEnabled by calling CoGetCallContext to obtain a pointer to ISecurityCallContext and then calling IsSecurityEnabled. A brief example follows:

ISecurityCallContext* pSecCtx;
VARIANT_BOOL bIsEnabled;

HRESULT hr1 = CoGetCallContext(IID_ISecurityCallContext, (void**)&pSecCtx);
if (FAILED(hr1)) throw(hr1);
if (NULL == pSecCtx) {
    // Display error message.
    return E_FAIL;
}

HRESULT hr2 = pSecCtx->IsSecurityEnabled(&bIsEnabled);
return hr2;

Although the preferred way to call IsSecurityEnabled is by using the security call context object, you can also call IsSecurityEnabled through the object context. (See ObjectContext or IObjectContext for more information.)

Accessing Security Call Context Information

Checking Role Membership

Programmatic Component Security