Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the SID of the current Windows account?

I am looking for an easy way to get the SID for the current Windows user account. I know I can do it through WMI, but I don't want to go that route.

Apologies to everybody that answered in C# for not specifying it's C++. :-)

like image 577
Franci Penov Avatar asked Oct 30 '08 18:10

Franci Penov


People also ask

How do I find my Windows SID?

Here are detailed steps. Step 1: Run Command Prompt as administrator in the search box. Step 2: In the elevated window, type wmic useraccount get name, sid and hit Enter to execute the command. Wait for a while, and then you will get the result.

What is Windows account SID?

In the context of Windows computing and Microsoft Active Directory (AD), a security identifier (SID) is a unique value that is used to identify any security entity that the Windows operating system (OS) can authenticate.

Does Windows 10 have a SID?

SID or Security Identifier is defined as a string value, which is associated with every single Windows user account. Just like the user account name, SID is also another parameter to recognize a user account in Windows 10.


1 Answers

In Win32, call GetTokenInformation, passing a token handle and the TokenUser constant. It will fill in a TOKEN_USER structure for you. One of the elements in there is the user's SID. It's a BLOB (binary), but you can turn it into a string by using ConvertSidToStringSid.

To get hold of the current token handle, use OpenThreadToken or OpenProcessToken.

If you prefer ATL, it has the CAccessToken class, which has all sorts of interesting things in it.

.NET has the Thread.CurrentPrinciple property, which returns an IPrincipal reference. You can get the SID:

IPrincipal principal = Thread.CurrentPrincipal; WindowsIdentity identity = principal.Identity as WindowsIdentity; if (identity != null)     Console.WriteLine(identity.User); 

Also in .NET, you can use WindowsIdentity.GetCurrent(), which returns the current user ID:

WindowsIdentity identity = WindowsIdentity.GetCurrent(); if (identity != null)     Console.WriteLine(identity.User); 
like image 93
Roger Lipscombe Avatar answered Sep 19 '22 23:09

Roger Lipscombe