|
-
Jan 20th, 2005, 06:34 PM
#1
Thread Starter
Member
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?
-
Jan 20th, 2005, 08:38 PM
#2
Addicted Member
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 !
-
Jan 23rd, 2005, 04:55 PM
#3
Thread Starter
Member
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????
-
Jan 24th, 2005, 01:11 PM
#4
Addicted Member
Re: Get User SID using code
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))
There are no stupid questions, but a whole bunch of dumb sayings !
Save time on database code, try DataLG !
-
Jan 25th, 2005, 12:51 AM
#5
Thread Starter
Member
Re: Get User SID using code
Thanks heaps sixfeetsix. Awesome !!
-
Dec 29th, 2006, 10:27 PM
#6
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|