Results 1 to 6 of 6

Thread: Get User SID using code

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52

    Get User SID using code

    I'm trying to get the user SID through code. I have got some VB 6 code to do this but cannot seem to get it to work in .NET. Anyone already got this working?

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Location
    Montréal
    Posts
    160

    Re: Get User SID using code

    You'll have to do some translation :

    Code:
    using System;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Security.Principal;
    
        public class UserSid
        {
            [DllImport("advapi32", CharSet=CharSet.Auto, SetLastError=true)]
            static extern bool LookupAccountSid
            (
                string lpSystemName,
                IntPtr pSid,
                StringBuilder Account,
                ref int cbName,
                StringBuilder DomainName,
                ref int cbDomainName,
                ref int peUse
            );
    
            [DllImport("advapi32", CharSet=CharSet.Auto)] 
            static extern bool GetTokenInformation
            (
                IntPtr TokenHandle, 
                TOKEN_INFORMATION_CLASS TokenInformationClass, 
                IntPtr TokenInformation, 
                int TokenInformationLength, 
                ref int ReturnLength 
            ); 
    
            [DllImport("advapi32", CharSet=CharSet.Auto)] 
            static extern bool ConvertSidToStringSid
            (
                IntPtr psid, 
                [In, Out, MarshalAs(UnmanagedType.LPTStr)] ref string pStringSid
            );  
    
            public struct SID_AND_ATTRIBUTES 
            { 
                public IntPtr Sid; 
                public int Attributes; 
            } 
    
            public struct TOKEN_USER 
            { 
                public SID_AND_ATTRIBUTES User; 
            } 
    
            public enum TOKEN_INFORMATION_CLASS 
            { 
                TokenUser = 1, 
                TokenGroups, 
                TokenPrivileges, 
                TokenOwner, 
                TokenPrimaryGroup, 
                TokenDefaultDacl, 
                TokenSource, 
                TokenType, 
                TokenImpersonationLevel, 
                TokenStatistics, 
                TokenRestrictedSids, 
                TokenSessionId, 
                TokenGroupsAndPrivileges, 
                TokenSessionReference, 
                TokenSandBoxInert, 
                TokenAuditPolicy, 
                TokenOrigin 
            } 
    
            public string GetUserSid(System.IntPtr userToken)
            {
                TOKEN_USER TokenUser; 
                int TokenInfLength = 0 ; 
                string sUserSid = "";
    
                GetTokenInformation(userToken, 
                    TOKEN_INFORMATION_CLASS.TokenUser,
                    IntPtr.Zero,
                    TokenInfLength,
                    ref TokenInfLength); 
    
                IntPtr TokenInformation = Marshal.AllocHGlobal(TokenInfLength ); 
    
                GetTokenInformation(userToken, 
                    TOKEN_INFORMATION_CLASS.TokenUser, 
                    TokenInformation, 
                    TokenInfLength,
                    ref TokenInfLength);
    
                TokenUser = (TOKEN_USER)Marshal.PtrToStructure(TokenInformation, 
                                                               typeof(TOKEN_USER)); 
    
                ConvertSidToStringSid(TokenUser.User.Sid, ref sUserSid);
    
                return sUserSid;
            }
        }
    Here is how I get my sid using the class above :

    Code:
    using System.Security.Principal;
    
    	MessageBox.Show(myUserSid.GetUserSid(WindowsIdentity.GetCurrent().Token));
    Good luck !
    Last edited by sixfeetsix; Jan 20th, 2005 at 08:48 PM.
    There are no stupid questions, but a whole bunch of dumb sayings !

    Save time on database code, try DataLG !

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52

    Re: Get User SID using code

    I have no idea how to convert to VB .NET. Anyone handy in both languages that is willing to help????

  4. #4
    Addicted Member
    Join Date
    Jan 2005
    Location
    Montréal
    Posts
    160

    Re: Get User SID using code

    VB Code:
    1. Imports System
    2. Imports System.Runtime.InteropServices
    3. Imports System.Text
    4. Imports System.Security.Principal
    5.  
    6. Public Class UserSid
    7.  
    8.     Declare Auto Function GetTokenInformation _
    9.         Lib "advapi32" ( _
    10.         ByVal TokenHandle As IntPtr, _
    11.         ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, _
    12.         ByVal TokenInformation As IntPtr, _
    13.         ByVal TokenInformationLength As Int32, _
    14.         ByRef ReturnLength As Int32 _
    15.     ) As Boolean
    16.  
    17.     Declare Auto Function ConvertSidToStringSid _
    18.         Lib "advapi32" ( _
    19.         ByVal psid As IntPtr, _
    20.         <InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.LPTStr)> _
    21.         ByRef pStringSid As String _
    22.     ) As Boolean
    23.  
    24.     Public Structure SID_AND_ATTRIBUTES
    25.         Public Sid As IntPtr
    26.         Public Attributes As Int32
    27.     End Structure
    28.  
    29.     Public Structure TOKEN_USER
    30.         Public User As SID_AND_ATTRIBUTES
    31.     End Structure
    32.  
    33.     Public Enum TOKEN_INFORMATION_CLASS
    34.         TokenUser = 1
    35.         TokenGroups
    36.         TokenPrivileges
    37.         TokenOwner
    38.         TokenPrimaryGroup
    39.         TokenDefaultDacl
    40.         TokenSource
    41.         TokenType
    42.         TokenImpersonationLevel
    43.         TokenStatistics
    44.         TokenRestrictedSids
    45.         TokenSessionId
    46.         TokenGroupsAndPrivileges
    47.         TokenSessionReference
    48.         TokenSandBoxInert
    49.         TokenAuditPolicy
    50.         TokenOrigin
    51.     End Enum
    52.  
    53.     Public Function GetUserSid(ByVal userToken As IntPtr) As String
    54.  
    55.         Dim TokenUser As TOKEN_USER
    56.         Dim TokenInfLength As Integer = 0
    57.         Dim sUserSid As String = ""
    58.         Dim TokenInformation As IntPtr
    59.  
    60.         GetTokenInformation(userToken, _
    61.                             TOKEN_INFORMATION_CLASS.TokenUser, _
    62.                             IntPtr.Zero, _
    63.                             TokenInfLength, _
    64.                             TokenInfLength)
    65.  
    66.         TokenInformation = Marshal.AllocHGlobal(TokenInfLength)
    67.  
    68.         GetTokenInformation(userToken, _
    69.                             TOKEN_INFORMATION_CLASS.TokenUser, _
    70.                             TokenInformation, _
    71.                             TokenInfLength, _
    72.                             TokenInfLength)
    73.  
    74.         TokenUser = DirectCast(Marshal.PtrToStructure( _
    75.                                     TokenInformation, _
    76.                                     TokenUser.GetType), TOKEN_USER)
    77.  
    78.         ConvertSidToStringSid(TokenUser.User.Sid, sUserSid)
    79.  
    80.         Return sUserSid
    81.  
    82.     End Function
    83.  
    84. End Class

    VB Code:
    1. Imports System.Security.Principal
    2.  
    3.         Dim myUserSid As New UserSid
    4.  
    5.         MessageBox.Show(myUserSid.GetUserSid(WindowsIdentity.GetCurrent().Token))
    There are no stupid questions, but a whole bunch of dumb sayings !

    Save time on database code, try DataLG !

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2001
    Posts
    52

    Re: Get User SID using code

    Thanks heaps sixfeetsix. Awesome !!

  6. #6
    Member
    Join Date
    Dec 2004
    Posts
    39

    Re: Get User SID using code

    How can I use the same code to get a different user's SID, rather than the current loged in user.

    I was thinking of using a listview to display the windows users, and when you clicked on the user, the selected user's SID will show.

    Can anyone show me how to do that?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width