Authorization Functions


CheckTokenMembership Function

The CheckTokenMembership function determines whether a specified security identifier (SID) is enabled in an access token.

Syntax

C++
BOOL WINAPI CheckTokenMembership(
  __in_opt  HANDLE TokenHandle,
  __in      PSID SidToCheck,
  __out     PBOOL IsMember
);

Parameters

TokenHandle [in, optional]

A handle to an access token. The handle must have TOKEN_QUERY access to the token. The token must be an impersonation token.

If TokenHandle is NULL, CheckTokenMembership uses the impersonation token of the calling thread. If the thread is not impersonating, the function duplicates the thread's primary token to create an impersonation token.

SidToCheck [in]

A pointer to a SID structure. The CheckTokenMembership function checks for the presence of this SID in the user and group SIDs of the access token.

IsMember [out]

A pointer to a variable that receives the results of the check. If the SID is present and has the SE_GROUP_ENABLED attribute, IsMember returns TRUE; otherwise, it returns FALSE.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The CheckTokenMembership function simplifies the process of determining whether a SID is both present and enabled in an access token.

Even if a SID is present in the token, the system may not use the SID in an access check. The SID may be disabled or have the SE_GROUP_USE_FOR_DENY_ONLY attribute. The system uses only enabled SIDs to grant access when performing an access check. For more information, see SID Attributes in an Access Token.

If TokenHandle is a restricted token, or if TokenHandle is NULL and the current effective token of the calling thread is a restricted token, CheckTokenMembership also checks whether the SID is present in the list of restricting SIDs.

Examples

The following example shows checking a token for membership in the Administrators local group.

BOOL IsUserAdmin(VOID)
/*++ 
Routine Description: This routine returns TRUE if the caller's
process is a member of the Administrators local group. Caller is NOT
expected to be impersonating anyone and is expected to be able to
open its own process and process token. 
Arguments: None. 
Return Value: 
   TRUE - Caller has Administrators local group. 
   FALSE - Caller does not have Administrators local group. --
*/ 
{
BOOL b;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
PSID AdministratorsGroup; 
b = AllocateAndInitializeSid(
    &NtAuthority,
    2,
    SECURITY_BUILTIN_DOMAIN_RID,
    DOMAIN_ALIAS_RID_ADMINS,
    0, 0, 0, 0, 0, 0,
    &AdministratorsGroup); 
if(b) 
{
    if (!CheckTokenMembership( NULL, AdministratorsGroup, &b)) 
    {
         b = FALSE;
    } 
    FreeSid(AdministratorsGroup); 
}

return(b);
}

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinbase.h (include Windows.h)
LibraryAdvapi32.lib
DLLAdvapi32.dll

See Also

Access Control Overview
Basic Access Control Functions
AccessCheck
CreateRestrictedToken

Send comments about this topic to Microsoft

Build date: 9/11/2009

Tags :


Community Content

tchao
Remarks need to be updated for Windows Vista and later OS

"The CheckTokenMembership function simplifies the process of determining whether a SID is both present and enabled in an access token"--this statement is incorrect when it comes to checking the local administrators group with UAC enabled in Windows Vista.

When UAC is enabled in Windows Vista--which is the default setup, a thread in an administrator account will have a pair of split tokens: a filtered token and an elevated token. The filtered token will have the local administrators group SID in its group, but that SID is not enabled until the thread gets the elevated token after user's approval via the UAC dialog or programmatically. The above sample code shows that both a filtered administrator token and an elevated administrator token as having the local administrators group SID "enabled," but that is not the case with the filtered administrator token which has its TOKEN_ELEVATION_TYPE as TokenElevationTypeLimited.

If you look at the local administrators group association with the administrator filtered token, it's for deny only, but CheckTokenMembership() will show that the administrator filtered token is a member (enabled?) of the local administrators group. Perhaps this is also a function implementation bug?!

Tags : contentbug

Page view tracker