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

后端 未结 11 1124
面向向阳花
面向向阳花 2020-12-14 01:02

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 ev

相关标签:
11条回答
  • 2020-12-14 01:41
    ATL::CAccessToken accessToken;
    ATL::CSid currentUserSid;
    if (accessToken.GetProcessToken(TOKEN_READ | TOKEN_QUERY) &&
        accessToken.GetUser(&currentUserSid))
        return currentUserSid.Sid();
    
    0 讨论(0)
  • 2020-12-14 01:43

    I found another way to get SID:

    System.Security.Principal.WindowsIdentity id = System.Security.Principal.WindowsIdentity.GetCurrent();
    string sid = id.User.AccountDomainSid.ToString();
    
    0 讨论(0)
  • 2020-12-14 01:44

    in cmd.exe

    whoami /user

    if you need it programatically, please ask more specified

    0 讨论(0)
  • 2020-12-14 01:45

    This should give you what you need:

    using System.Security.Principal;

    ...

    var sid = WindowsIdentity.GetCurrent().User;

    The User property of WindowsIdentity returns the SID, per MSDN Docs

    0 讨论(0)
  • 2020-12-14 01:51

    This one is the shortest of them all I believe.

    UserPrincipal.Current.Sid;
    

    Available with .net >= 3.5

    0 讨论(0)
提交回复
热议问题