Getting the Logon SID in C++

logon security identifier (SID) identifies the logon session associated with an access token. A typical use of a logon SID is in an ACE that allows access for the duration of a client's logon session. For example, a Windows service can use the LogonUser function to start a new logon session. The LogonUser function returns an access token from which the service can extract the logon SID. The service can then use the SID in an ACE that allows the client's logon session to access the interactive window station and desktop.

The following example gets the logon SID from an access token. It uses the GetTokenInformation function to fill a TOKEN_GROUPS buffer with an array of the group SIDs from an access token. This array includes the logon SID, which is identified by the SE_GROUP_LOGON_ID attribute. The example function allocates a buffer for the logon SID; it is the caller's responsibility to free the buffer.

None.gif BOOL GetLogonSID (HANDLE hToken, PSID  * ppsid) 
ExpandedBlockStart.gif  {
InBlock.gif   BOOL bSuccess  =  FALSE;
InBlock.gif   DWORD dwIndex;
InBlock.gif   DWORD dwLength  =   0 ;
InBlock.gif   PTOKEN_GROUPS ptg  =  NULL;
InBlock.gif
InBlock.gif //  Verify the parameter passed in is not NULL. 
InBlock.gif 
     if  (NULL  ==  ppsid)
InBlock.gif         goto  Cleanup;
InBlock.gif
InBlock.gif //  Get required buffer size and allocate the TOKEN_GROUPS buffer. 
InBlock.gif 

InBlock.gif    if  ( ! GetTokenInformation(
InBlock.gif         hToken,          //  handle to the access token 
InBlock.gif 
         TokenGroups,     //  get information about the token's groups  
InBlock.gif 
         (LPVOID) ptg,    //  pointer to TOKEN_GROUPS buffer 
InBlock.gif 
          0 ,               //  size of buffer 
InBlock.gif 
          & dwLength        //  receives required buffer size 
InBlock.gif 
      )) 
ExpandedSubBlockStart.gif    {
InBlock.gif       if  (GetLastError()  !=  ERROR_INSUFFICIENT_BUFFER) 
InBlock.gif          goto  Cleanup;
InBlock.gif
InBlock.gif      ptg  =  (PTOKEN_GROUPS)HeapAlloc(GetProcessHeap(),
InBlock.gif         HEAP_ZERO_MEMORY, dwLength);
InBlock.gif
InBlock.gif       if  (ptg  ==  NULL)
InBlock.gif          goto  Cleanup;
ExpandedSubBlockEnd.gif   } 
InBlock.gif 
InBlock.gif //  Get the token group information from the access token. 
InBlock.gif 

InBlock.gif    if  ( ! GetTokenInformation(
InBlock.gif         hToken,          //  handle to the access token 
InBlock.gif 
         TokenGroups,     //  get information about the token's groups  
InBlock.gif 
         (LPVOID) ptg,    //  pointer to TOKEN_GROUPS buffer 
InBlock.gif 
         dwLength,        //  size of buffer 
InBlock.gif 
          & dwLength        //  receives required buffer size 
InBlock.gif 
         )) 
ExpandedSubBlockStart.gif    {
InBlock.gif       goto  Cleanup;
ExpandedSubBlockEnd.gif   } 
InBlock.gif 
InBlock.gif //  Loop through the groups to find the logon SID. 
InBlock.gif 

InBlock.gif    for  (dwIndex  =   0 ; dwIndex  <  ptg -> GroupCount; dwIndex ++ ) 
InBlock.gif       if  ((ptg -> Groups[dwIndex].Attributes  &  SE_GROUP_LOGON_ID)
InBlock.gif              ==   SE_GROUP_LOGON_ID) 
ExpandedSubBlockStart.gif       {
InBlock.gif       //  Found the logon SID; make a copy of it. 
InBlock.gif 

InBlock.gif         dwLength  =  GetLengthSid(ptg -> Groups[dwIndex].Sid);
InBlock.gif          * ppsid  =  (PSID) HeapAlloc(GetProcessHeap(),
InBlock.gif                     HEAP_ZERO_MEMORY, dwLength);
InBlock.gif          if  ( * ppsid  ==  NULL)
InBlock.gif              goto  Cleanup;
InBlock.gif          if  ( ! CopySid(dwLength,  * ppsid, ptg -> Groups[dwIndex].Sid)) 
ExpandedSubBlockStart.gif          {
InBlock.gif             HeapFree(GetProcessHeap(),  0 , (LPVOID) * ppsid);
InBlock.gif              goto  Cleanup;
ExpandedSubBlockEnd.gif         } 
InBlock.gif          break ;
ExpandedSubBlockEnd.gif      } 
InBlock.gif 
InBlock.gif   bSuccess  =  TRUE;
InBlock.gif
InBlock.gifCleanup: 
InBlock.gif
InBlock.gif //  Free the buffer for the token groups. 
InBlock.gif 

InBlock.gif    if  (ptg  !=  NULL)
InBlock.gif      HeapFree(GetProcessHeap(),  0 , (LPVOID)ptg);
InBlock.gif
InBlock.gif    return  bSuccess;
ExpandedBlockEnd.gif
None.gif

The following function frees the buffer allocated by the  GetLogonSID  example function.
None.gifVOID FreeLogonSID (PSID *ppsid) 
ExpandedBlockStart.gif {
InBlock.gif    HeapFree(GetProcessHeap(), 0, (LPVOID)*ppsid);
ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值