Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 564853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:48:37+00:00 2026-05-13T12:48:37+00:00

How does one retrieve the Windows Logon SID in C# .net? (not the user

  • 0

How does one retrieve the Windows Logon SID in C# .net? (not the user SID, but the unique new one for each session)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:48:37+00:00Added an answer on May 13, 2026 at 12:48 pm

    I’m afraid you have to resort to using P/Invoke. There’s an example how to do it at pinvoke.net (please see the bottom of the page):

    Result = GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenSessionId , TokenInformation , TokenInfLength , out TokenInfLength );
    

    Please note that I changed the example by altering just one line, I replaced TOKEN_INFORMATION_CLASS.TokenUser with TOKEN_INFORMATION_CLASS.TokenSessionId which is exactly what you need.

    Hope this helps.

    Update: Here’s the working (at least on my machine) code:

    using System;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    
    namespace LinqTest
    {
        public class ClsLookupAccountName
        {
            public const uint SE_GROUP_LOGON_ID = 0xC0000000; // from winnt.h
            public const int TokenGroups = 2; // from TOKEN_INFORMATION_CLASS
    
            enum TOKEN_INFORMATION_CLASS
            {
                TokenUser = 1,
                TokenGroups,
                TokenPrivileges,
                TokenOwner,
                TokenPrimaryGroup,
                TokenDefaultDacl,
                TokenSource,
                TokenType,
                TokenImpersonationLevel,
                TokenStatistics,
                TokenRestrictedSids,
                TokenSessionId,
                TokenGroupsAndPrivileges,
                TokenSessionReference,
                TokenSandBoxInert,
                TokenAuditPolicy,
                TokenOrigin
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct SID_AND_ATTRIBUTES
            {
                public IntPtr Sid;
                public uint Attributes;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            public struct TOKEN_GROUPS
            {
                public int GroupCount;
                [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)]
                public SID_AND_ATTRIBUTES[] Groups;
            };
    
            // Using IntPtr for pSID instead of Byte[]
            [DllImport("advapi32", CharSet = CharSet.Auto, SetLastError = true)]
            static extern bool ConvertSidToStringSid(IntPtr pSID, out IntPtr ptrSid);
    
            [DllImport("kernel32.dll")]
            static extern IntPtr LocalFree(IntPtr hMem);
    
            [DllImport("advapi32.dll", SetLastError = true)]
            static extern bool GetTokenInformation(
                IntPtr TokenHandle,
                TOKEN_INFORMATION_CLASS TokenInformationClass,
                IntPtr TokenInformation,
                int TokenInformationLength,
                out int ReturnLength);
    
            public static string GetLogonId()
            {
                int TokenInfLength = 0;
                // first call gets lenght of TokenInformation
                bool Result = GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenGroups, IntPtr.Zero, TokenInfLength, out TokenInfLength);
                IntPtr TokenInformation = Marshal.AllocHGlobal(TokenInfLength);
                Result = GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenGroups, TokenInformation, TokenInfLength, out TokenInfLength);
    
                if (!Result)
                {
                    Marshal.FreeHGlobal(TokenInformation);
                    return string.Empty;
                }
    
                string retVal = string.Empty;
                TOKEN_GROUPS groups = (TOKEN_GROUPS)Marshal.PtrToStructure(TokenInformation, typeof(TOKEN_GROUPS));
                int sidAndAttrSize = Marshal.SizeOf(new SID_AND_ATTRIBUTES());
                for (int i = 0; i < groups.GroupCount; i++)
                {
                    SID_AND_ATTRIBUTES sidAndAttributes = (SID_AND_ATTRIBUTES)Marshal.PtrToStructure(
                        new IntPtr(TokenInformation.ToInt64() + i * sidAndAttrSize + IntPtr.Size), typeof(SID_AND_ATTRIBUTES));
                    if ((sidAndAttributes.Attributes & SE_GROUP_LOGON_ID) == SE_GROUP_LOGON_ID)
                    {
                        IntPtr pstr = IntPtr.Zero;
                        ConvertSidToStringSid(sidAndAttributes.Sid, out pstr);
                        retVal = Marshal.PtrToStringAuto(pstr);
                        LocalFree(pstr);
                        break;
                    }
                }
    
                Marshal.FreeHGlobal(TokenInformation);
                return retVal;
            }
        }
    }
    

    N.B. I tested it on my x64 machine, so please pay close attention on TokenInformation.ToInt64() piece of code, maybe you should replace it with TokenInformation.ToInt32()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does one retrieve a network share comment in Windows ?
How does one handle a DateTime with a NOT NULL ? I want to
How does one start development in Silverlight? Does one need a new IDE? or
How does one go about authoring a Regular Expression that matches against all strings
How does one go about referencing a class's static properties in xaml? In other
How does one convert an image from one color profile to another (screen to
How does one go about programatically building a TemplateColumn object and adding it to
How does one invoke a groovy method that prints to stdout, appending the output
How does one do this? If I want to analyze how something is getting
How does one get a list of running applications as shown in the "Applications"

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.