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?
Printable View
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?
You'll have to do some translation :
Here is how I get my sid using the class above :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;
}
}
Good luck !Code:using System.Security.Principal;
MessageBox.Show(myUserSid.GetUserSid(WindowsIdentity.GetCurrent().Token));
I have no idea how to convert to VB .NET. Anyone handy in both languages that is willing to help????
VB Code:
Imports System Imports System.Runtime.InteropServices Imports System.Text Imports System.Security.Principal Public Class UserSid Declare Auto Function GetTokenInformation _ Lib "advapi32" ( _ ByVal TokenHandle As IntPtr, _ ByVal TokenInformationClass As TOKEN_INFORMATION_CLASS, _ ByVal TokenInformation As IntPtr, _ ByVal TokenInformationLength As Int32, _ ByRef ReturnLength As Int32 _ ) As Boolean Declare Auto Function ConvertSidToStringSid _ Lib "advapi32" ( _ ByVal psid As IntPtr, _ <InAttribute(), OutAttribute(), MarshalAs(UnmanagedType.LPTStr)> _ ByRef pStringSid As String _ ) As Boolean Public Structure SID_AND_ATTRIBUTES Public Sid As IntPtr Public Attributes As Int32 End Structure Public Structure TOKEN_USER Public User As SID_AND_ATTRIBUTES End Structure Public Enum TOKEN_INFORMATION_CLASS TokenUser = 1 TokenGroups TokenPrivileges TokenOwner TokenPrimaryGroup TokenDefaultDacl TokenSource TokenType TokenImpersonationLevel TokenStatistics TokenRestrictedSids TokenSessionId TokenGroupsAndPrivileges TokenSessionReference TokenSandBoxInert TokenAuditPolicy TokenOrigin End Enum Public Function GetUserSid(ByVal userToken As IntPtr) As String Dim TokenUser As TOKEN_USER Dim TokenInfLength As Integer = 0 Dim sUserSid As String = "" Dim TokenInformation As IntPtr GetTokenInformation(userToken, _ TOKEN_INFORMATION_CLASS.TokenUser, _ IntPtr.Zero, _ TokenInfLength, _ TokenInfLength) TokenInformation = Marshal.AllocHGlobal(TokenInfLength) GetTokenInformation(userToken, _ TOKEN_INFORMATION_CLASS.TokenUser, _ TokenInformation, _ TokenInfLength, _ TokenInfLength) TokenUser = DirectCast(Marshal.PtrToStructure( _ TokenInformation, _ TokenUser.GetType), TOKEN_USER) ConvertSidToStringSid(TokenUser.User.Sid, sUserSid) Return sUserSid End Function End Class
VB Code:
Imports System.Security.Principal Dim myUserSid As New UserSid MessageBox.Show(myUserSid.GetUserSid(WindowsIdentity.GetCurrent().Token))
Thanks heaps sixfeetsix. Awesome !! :thumb:
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?