33// The .NET Foundation licenses this file to you under the MIT license.
44// -----------------------------------------------------------------------
55
6- using Kerberos . NET . Client ;
7- using Kerberos . NET . Entities ;
86using System ;
97using System . Collections . Generic ;
108using System . Runtime . InteropServices ;
119using System . Security . Cryptography ;
1210using System . Text ;
11+ using Kerberos . NET . Client ;
12+ using Kerberos . NET . Entities ;
1313using static Kerberos . NET . Win32 . NativeMethods ;
1414
1515namespace Kerberos . NET . Win32
1616{
17+ [ Flags ]
18+ public enum LsaMode
19+ {
20+ SameProcess = 1 << 16 ,
21+ MarshallingNeeded = 1 << 17
22+ }
23+
1724 /// <summary>
1825 /// Provides a layer to interact with the LSA functions used to create logon sessions and manipulate the ticket caches.
1926 /// </summary>
@@ -29,7 +36,6 @@ public class LsaInterop : IDisposable
2936 private readonly int negotiateAuthPackage ;
3037
3138 private LsaTokenSafeHandle impersonationContext ;
32- private LUID luid ;
3339
3440 private bool disposedValue ;
3541
@@ -54,9 +60,10 @@ public class LsaInterop : IDisposable
5460 * pool of memory to create a working for the current operation. On dispose it zeros the memory and returns it to the pool.
5561 */
5662
57- private LsaInterop ( LsaSafeHandle lsaHandle , string packageName = KerberosPackageName )
63+ private LsaInterop ( LsaSafeHandle lsaHandle , string packageName = KerberosPackageName , LsaMode securityMode = default )
5864 {
5965 this . lsaHandle = lsaHandle ;
66+ this . SecurityMode = securityMode ;
6067
6168 var kerberosPackageName = new LSA_STRING
6269 {
@@ -79,6 +86,13 @@ private LsaInterop(LsaSafeHandle lsaHandle, string packageName = KerberosPackage
7986 LsaThrowIfError ( result ) ;
8087 }
8188
89+ /// <summary>
90+ /// The current LogonId represented by this LSA Handle.
91+ /// </summary>
92+ public ulong LogonId => this . impersonationContext ? . Luid ?? 0 ;
93+
94+ public LsaMode SecurityMode { get ; }
95+
8296 /// <summary>
8397 /// Create a new instance of the interop and allow this instance to behave as SYSTEM.
8498 /// Note that this call requires the TrustedComputingBase privilege to execute.
@@ -88,16 +102,7 @@ private LsaInterop(LsaSafeHandle lsaHandle, string packageName = KerberosPackage
88102 /// <returns>Returns an instance of the <see cref="LsaInterop"/> class.</returns>
89103 public static LsaInterop RegisterLogonProcess ( string name = null , string package = KerberosPackageName )
90104 {
91- string processNameStr ;
92-
93- if ( string . IsNullOrWhiteSpace ( name ) )
94- {
95- processNameStr = ProcessName ;
96- }
97- else
98- {
99- processNameStr = name ;
100- }
105+ string processNameStr = string . IsNullOrWhiteSpace ( name ) ? ProcessName : name ;
101106
102107 if ( string . IsNullOrWhiteSpace ( package ) )
103108 {
@@ -111,11 +116,11 @@ public static LsaInterop RegisterLogonProcess(string name = null, string package
111116 MaximumLength = ( ushort ) processNameStr . Length
112117 } ;
113118
114- var result = LsaRegisterLogonProcess ( ref processName , out LsaSafeHandle lsaHandle , out ulong securityMode ) ;
119+ var result = LsaRegisterLogonProcess ( ref processName , out LsaSafeHandle lsaHandle , out LsaMode securityMode ) ;
115120
116121 LsaThrowIfError ( result ) ;
117122
118- return new LsaInterop ( lsaHandle , package ) ;
123+ return new LsaInterop ( lsaHandle , package , securityMode ) ;
119124 }
120125
121126 /// <summary>
@@ -137,11 +142,6 @@ public static LsaInterop Connect(string package = KerberosPackageName)
137142 return new LsaInterop ( lsaHandle , package ) ;
138143 }
139144
140- /// <summary>
141- /// The current LogonId represented by this LSA Handle.
142- /// </summary>
143- public ulong LogonId => this . luid ;
144-
145145 /// <summary>
146146 /// Create a "NewCredentials" logon session for the current LSA Handle. This does not authenticate the user
147147 /// and only uses the credentials provided for outbound calls similar to the /netonly flag for runas.exe.
@@ -237,13 +237,15 @@ LogonType logonType
237237 ref tokenSource ,
238238 out profileBuffer ,
239239 ref profileLength ,
240- out this . luid ,
240+ out LUID luid ,
241241 out tokenHandle ,
242242 out IntPtr pQuotas ,
243243 out int subStatus
244244 ) ;
245245
246246 LsaThrowIfError ( result ) ;
247+
248+ tokenHandle . Luid = luid ;
247249 }
248250 finally
249251 {
0 commit comments