Skip to content

Commit 47e3b52

Browse files
authored
Merge pull request #351 from dotnet/feature/logonuser-param
Expose luid based on impersonation instead of whatever last logon was
2 parents 0f8ef1a + e28437f commit 47e3b52

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

Kerberos.NET/Win32/LsaInterop.cs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@
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;
86
using System;
97
using System.Collections.Generic;
108
using System.Runtime.InteropServices;
119
using System.Security.Cryptography;
1210
using System.Text;
11+
using Kerberos.NET.Client;
12+
using Kerberos.NET.Entities;
1313
using static Kerberos.NET.Win32.NativeMethods;
1414

1515
namespace 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
{

Kerberos.NET/Win32/LsaTokenSafeHandle.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public LsaTokenSafeHandle()
2121

2222
public override bool IsInvalid => this.handle == IntPtr.Zero;
2323

24+
public ulong Luid { get; internal set; }
25+
2426
protected override bool ReleaseHandle()
2527
{
2628
this.Revert();

Kerberos.NET/Win32/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ [Out] out LsaSafeHandle LsaHandle
168168
public static extern int LsaRegisterLogonProcess(
169169
ref LSA_STRING LogonProcessName,
170170
out LsaSafeHandle LsaHandle,
171-
out ulong SecurityMode
171+
out LsaMode SecurityMode
172172
);
173173

174174
[DllImport(SECUR32)]

0 commit comments

Comments
 (0)