Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm afraid you have to resort to using P/Invoke. There's an example how to do it at <a href="http://www.pinvoke.net/default.aspx/advapi32.GetTokenInformation" rel="noreferrer">pinvoke.net</a> (please see the bottom of the page):</p> <pre><code>Result = GetTokenInformation(WindowsIdentity.GetCurrent().Token, TOKEN_INFORMATION_CLASS.TokenSessionId , TokenInformation , TokenInfLength , out TokenInfLength ); </code></pre> <p>Please note that I changed the example by altering just one line, I replaced <code>TOKEN_INFORMATION_CLASS.TokenUser</code> with <code>TOKEN_INFORMATION_CLASS.TokenSessionId</code> which is exactly what you need.</p> <p>Hope this helps.</p> <p><strong>Update: Here's the working (at least on my machine) code:</strong></p> <pre><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 &lt; 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 &amp; 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; } } } </code></pre> <p>N.B. I tested it on my x64 machine, so please pay close attention on <code>TokenInformation.ToInt64()</code> piece of code, maybe you should replace it with <code>TokenInformation.ToInt32()</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload