Skip to content

Commit c551f35

Browse files
authored
NLog v6 RTM (#12)
1 parent c1c8324 commit c551f35

8 files changed

Lines changed: 94 additions & 89 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ NLog extensions for displaying [User Windows Identity](https://github.com/NLog/N
1212
`Install-Package NLog.WindowsIdentity` or in your csproj:
1313

1414
```xml
15-
<PackageReference Include="NLog.WindowsIdentity" Version="5.*" />
15+
<PackageReference Include="NLog.WindowsIdentity" Version="6.*" />
1616
```
1717

1818
2) Add to your nlog.config:

appveyor.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 5.0.0.{build}
1+
version: 6.0.0.{build}
22
image: Visual Studio 2022
33
configuration: Release
44
platform: Any CPU
@@ -15,6 +15,6 @@ artifacts:
1515
deploy:
1616
- provider: NuGet
1717
api_key:
18-
secure: ACKSV1ixxNpO+2k8KvNDy6hd9QmR8lkQmKn773ZIIeVpG0ywYUhY4j8LcyykVR1a
18+
secure: f6oWebyOFLpuuo2PMd6xgoxwMq+JvXVUmPyBme89zS7UF0zcvLYPSKN/p6B/KaMs
1919
on:
2020
branch: master

src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ namespace NLog.Targets.Wrappers
5353
[Target("ImpersonatingWrapper", IsWrapper = true)]
5454
public class ImpersonatingTargetWrapper : WrapperTargetBase
5555
{
56-
private NewIdentityHandle _newIdentity;
56+
private readonly Action<AsyncLogEventInfo> _writeLogEvent;
57+
private readonly Action<IList<AsyncLogEventInfo>> _writeLogEvents;
58+
private NewIdentityHandle? _newIdentity;
5759

5860
/// <summary>
5961
/// Initializes a new instance of the <see cref="ImpersonatingTargetWrapper" /> class.
6062
/// </summary>
6163
public ImpersonatingTargetWrapper()
62-
: this(null)
6364
{
65+
_writeLogEvent = (l) => WrappedTarget?.WriteAsyncLogEvent(l);
66+
_writeLogEvents = (l) => WrappedTarget?.WriteAsyncLogEvents(l);
6467
}
6568

6669
/// <summary>
@@ -79,6 +82,7 @@ public ImpersonatingTargetWrapper(string name, Target wrappedTarget)
7982
/// </summary>
8083
/// <param name="wrappedTarget">The wrapped target.</param>
8184
public ImpersonatingTargetWrapper(Target wrappedTarget)
85+
: this()
8286
{
8387
WrappedTarget = wrappedTarget;
8488
}
@@ -90,13 +94,13 @@ public ImpersonatingTargetWrapper(Target wrappedTarget)
9094
/// If you use the user principal name (UPN) format, User@DNSDomainName, the <see cref="Domain"/> must be NULL.
9195
/// </remarks>
9296
/// <docgen category='Impersonation Options' order='10' />
93-
public Layout UserName { get; set; }
97+
public Layout? UserName { get; set; }
9498

9599
/// <summary>
96100
/// Gets or sets the user account password.
97101
/// </summary>
98102
/// <docgen category='Impersonation Options' order='10' />
99-
public Layout Password { get; set; }
103+
public Layout? Password { get; set; }
100104

101105
/// <summary>
102106
/// Gets or sets Windows domain name to change context to.
@@ -141,7 +145,7 @@ protected override void InitializeTarget()
141145
base.InitializeTarget();
142146
}
143147

144-
private NewIdentityHandle ResolveNewIdentity(LogEventInfo logEvent, bool forceCreate)
148+
private NewIdentityHandle? ResolveNewIdentity(LogEventInfo logEvent, bool forceCreate)
145149
{
146150
if (RevertToSelf)
147151
{
@@ -192,13 +196,9 @@ protected override void CloseTarget()
192196
/// <param name="logEvent">The log event.</param>
193197
protected override void Write(AsyncLogEventInfo logEvent)
194198
{
195-
if (_writeLogEvent is null)
196-
_writeLogEvent = (l) => WrappedTarget.WriteAsyncLogEvent(l);
197-
198199
var newIdentity = ResolveNewIdentity(logEvent.LogEvent, true);
199200
RunImpersonated(newIdentity, _writeLogEvent, logEvent);
200201
}
201-
private Action<AsyncLogEventInfo> _writeLogEvent;
202202

203203
/// <summary>
204204
/// Changes the security context, forwards the call to the <see cref="WrapperTargetBase.WrappedTarget"/>.Write()
@@ -207,17 +207,13 @@ protected override void Write(AsyncLogEventInfo logEvent)
207207
/// <param name="logEvents">Log events.</param>
208208
protected override void Write(IList<AsyncLogEventInfo> logEvents)
209209
{
210-
if (_writeLogEvents is null)
211-
_writeLogEvents = (l) => WrappedTarget.WriteAsyncLogEvents(l);
212-
213210
if (logEvents.Count > 0)
214211
{
215212
var firstLogEvent = logEvents[0].LogEvent;
216213
var newIdentity = ResolveNewIdentity(firstLogEvent, true);
217214
RunImpersonated(newIdentity, _writeLogEvents, logEvents);
218215
}
219216
}
220-
private Action<IList<AsyncLogEventInfo>> _writeLogEvents;
221217

222218
/// <summary>
223219
/// Flush any pending log messages (in case of asynchronous targets).
@@ -226,10 +222,10 @@ protected override void Write(IList<AsyncLogEventInfo> logEvents)
226222
protected override void FlushAsync(AsyncContinuation asyncContinuation)
227223
{
228224
var newIdentity = ResolveNewIdentity(LogEventInfo.CreateNullEvent(), true);
229-
RunImpersonated(newIdentity, (s) => WrappedTarget.Flush(s), asyncContinuation);
225+
RunImpersonated(newIdentity, (s) => WrappedTarget?.Flush(s), asyncContinuation);
230226
}
231227

232-
private void RunImpersonated<T>(NewIdentityHandle newIdentity, Action<T> executeOperation, T state)
228+
private static void RunImpersonated<T>(NewIdentityHandle? newIdentity, Action<T> executeOperation, T state)
233229
{
234230
NewIdentityHandle.RunImpersonated(newIdentity, executeOperation, state);
235231
}
@@ -240,19 +236,20 @@ internal sealed class NewIdentityHandle : IDisposable
240236
public string Domain { get; }
241237
public int Password { get; }
242238

243-
#if NETSTANDARD
244-
public Microsoft.Win32.SafeHandles.SafeAccessTokenHandle Handle { get; }
245-
#else
239+
#if NETFRAMEWORK
246240
public WindowsIdentity Handle { get; }
247241
private readonly IntPtr _handle = IntPtr.Zero;
248-
242+
#else
243+
public Microsoft.Win32.SafeHandles.SafeAccessTokenHandle Handle { get; }
249244
#endif
245+
250246
public NewIdentityHandle(string userName, string domain, string password, SecurityLogOnType logOnType, LogOnProviderType logOnProvider, SecurityImpersonationLevel impersonationLevel)
251247
{
252248
UserName = userName;
253249
Domain = domain;
254250
Password = password?.GetHashCode() ?? 0;
255251

252+
#pragma warning disable CS8604 // Possible null reference argument.
256253
if (!NativeMethods.LogonUser(
257254
userName,
258255
domain,
@@ -263,10 +260,9 @@ public NewIdentityHandle(string userName, string domain, string password, Securi
263260
{
264261
throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
265262
}
263+
#pragma warning restore CS8604 // Possible null reference argument.
266264

267-
#if NETSTANDARD
268-
Handle = logonHandle;
269-
#else
265+
#if NETFRAMEWORK
270266
// adapted from:
271267
// https://www.codeproject.com/csharp/cpimpersonation1.asp
272268
if (!NativeMethods.DuplicateToken(logonHandle, (int)impersonationLevel, out _handle))
@@ -279,6 +275,8 @@ public NewIdentityHandle(string userName, string domain, string password, Securi
279275

280276
// create new identity using new primary token)
281277
Handle = new WindowsIdentity(_handle);
278+
#else
279+
Handle = logonHandle;
282280
#endif
283281
}
284282

@@ -290,7 +288,7 @@ public bool IsValid(string userName, string domain, string password)
290288
public void Close()
291289
{
292290
Handle.Dispose();
293-
#if !NETSTANDARD
291+
#if NETFRAMEWORK
294292
if (_handle != IntPtr.Zero)
295293
NativeMethods.CloseHandle(_handle);
296294
#endif
@@ -301,12 +299,10 @@ public void Dispose()
301299
Close();
302300
}
303301

304-
internal static void RunImpersonated<T>(NewIdentityHandle newIdentity, Action<T> executeOperation, T state)
302+
internal static void RunImpersonated<T>(NewIdentityHandle? newIdentity, Action<T> executeOperation, T state)
305303
{
306-
#if NETSTANDARD
307-
WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
308-
#else
309-
WindowsImpersonationContext context = null;
304+
#if NETFRAMEWORK
305+
WindowsImpersonationContext? context = null;
310306
try
311307
{
312308
context = newIdentity?.Handle.Impersonate() ?? WindowsIdentity.Impersonate(IntPtr.Zero);
@@ -316,6 +312,8 @@ internal static void RunImpersonated<T>(NewIdentityHandle newIdentity, Action<T>
316312
{
317313
context?.Undo();
318314
}
315+
#else
316+
WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
319317
#endif
320318
}
321319
}

src/NLog.WindowsIdentity/NLog.WindowsIdentity.csproj

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' ">net46;net45;net35;netstandard1.5;netstandard2.0</TargetFrameworks>
4+
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' ">net46;net35;netstandard2.0;netstandard2.1</TargetFrameworks>
55
<RootNamespace>NLog</RootNamespace>
66

7-
<VersionPrefix>5.3.0</VersionPrefix>
7+
<VersionPrefix>6.0.0</VersionPrefix>
88
<VersionSuffix></VersionSuffix>
99

10-
<AssemblyVersion>5.0.0.0</AssemblyVersion>
11-
<!-- AssemblyVersion must be fixed on 5.0.0.0 -->
10+
<AssemblyVersion>6.0.0.0</AssemblyVersion>
11+
<!-- AssemblyVersion must be fixed on 6.0.0.0 -->
1212
<AppVeyorBuildNumber>$(APPVEYOR_BUILD_NUMBER)</AppVeyorBuildNumber>
1313
<AppVeyorBuildNumber Condition="'$(AppVeyorBuildNumber)' == ''">0</AppVeyorBuildNumber>
1414
<FileVersion>$(VersionPrefix).$(AppVeyorBuildNumber)</FileVersion>
@@ -23,6 +23,10 @@
2323
<Copyright>Copyright (c) 2004-$(CurrentYear) NLog Project - https://nlog-project.org/ </Copyright>
2424

2525
<PackageReleaseNotes>
26+
Changelog:
27+
- Update to NLog v6
28+
- Enable Nullable API
29+
2630
Windows-Identity LayoutRenderer Docs:
2731
- https://github.com/NLog/NLog/wiki/Windows-Identity-Layout-Renderer
2832
ImpersonatingWrapper Target Docs:
@@ -41,54 +45,47 @@ ImpersonatingWrapper Target Docs:
4145
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
4246

4347
<IsPackable>true</IsPackable>
48+
<!-- EmbedUntrackedSources for deterministic build -->
49+
<EmbedUntrackedSources>true</EmbedUntrackedSources>
4450
<GenerateDocumentationFile>true</GenerateDocumentationFile>
4551
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
4652
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
47-
<IsTrimmable>true</IsTrimmable>
48-
<!-- EmbedUntrackedSources for deterministic build -->
49-
<EmbedUntrackedSources>true</EmbedUntrackedSources>
53+
<IsTrimmable Condition=" '$(TargetFramework)' == 'netstandard2.1' ">true</IsTrimmable>
54+
<IsAotCompatible Condition=" '$(TargetFramework)' == 'netstandard2.1' ">true</IsAotCompatible>
55+
<Nullable>enable</Nullable>
56+
<LangVersion>9.0</LangVersion>
5057
</PropertyGroup>
5158

5259
<PropertyGroup Condition=" '$(TargetFramework)' == 'net46' ">
5360
<Title>NLog.WindowsIdentity for .NET Framework 4.6</Title>
5461
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
5562
</PropertyGroup>
5663

57-
<PropertyGroup Condition=" '$(TargetFramework)' == 'net45' ">
58-
<Title>NLog.WindowsIdentity for .NET Framework 4.5</Title>
59-
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
60-
</PropertyGroup>
61-
6264
<PropertyGroup Condition=" '$(TargetFramework)' == 'net35' ">
6365
<Title>NLog.WindowsIdentity for .NET Framework 3.5</Title>
6466
<DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
6567
</PropertyGroup>
6668

67-
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
68-
<Title>NLog.WindowsIdentity for NetStandard 1.5</Title>
69-
<NetStandardImplicitPackageVersion>1.6.0</NetStandardImplicitPackageVersion>
70-
</PropertyGroup>
71-
7269
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
7370
<Title>NLog.WindowsIdentity for NetStandard 2.0</Title>
7471
</PropertyGroup>
7572

76-
<ItemGroup Condition=" '$(TargetFramework)' == 'net46' or '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net35' ">
73+
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
74+
<Title>NLog.WindowsIdentity for NetStandard 2.1</Title>
75+
</PropertyGroup>
76+
77+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' == '.NETFramework' ">
7778
<Reference Include="System" />
7879
<Reference Include="System.Core" />
7980
</ItemGroup>
8081

81-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.5' ">
82-
<PackageReference Include="System.Security.Principal.Windows" Version="4.3.0" />
83-
</ItemGroup>
84-
85-
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
86-
<PackageReference Include="System.Security.Principal.Windows" Version="4.5.0" />
82+
<ItemGroup Condition=" '$(TargetFrameworkIdentifier)' != '.NETFramework' ">
83+
<PackageReference Include="System.Security.Principal.Windows" Version="4.7.0" />
8784
</ItemGroup>
8885

8986
<ItemGroup>
90-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
91-
<PackageReference Include="NLog" Version="5.2.2" />
87+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
88+
<PackageReference Include="NLog" Version="6.0.1" />
9289
</ItemGroup>
9390

9491
<PropertyGroup>

src/NLog.WindowsIdentity/NativeMethods.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,7 @@ namespace NLog.Internal
3838

3939
internal static class NativeMethods
4040
{
41-
#if NETSTANDARD
42-
// obtains user token
43-
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
44-
#if !NET35
45-
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
46-
#endif
47-
[return: MarshalAs(UnmanagedType.Bool)]
48-
internal static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, out Microsoft.Win32.SafeHandles.SafeAccessTokenHandle phToken);
49-
#else
41+
#if NETFRAMEWORK
5042
// obtains user token
5143
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
5244
#if !NET35
@@ -70,6 +62,12 @@ internal static class NativeMethods
7062
#endif
7163
[return: MarshalAs(UnmanagedType.Bool)]
7264
internal static extern bool DuplicateToken(IntPtr existingTokenHandle, int impersonationLevel, out IntPtr duplicateTokenHandle);
65+
#else
66+
// obtains user token
67+
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
68+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
69+
[return: MarshalAs(UnmanagedType.Bool)]
70+
internal static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, out Microsoft.Win32.SafeHandles.SafeAccessTokenHandle phToken);
7371
#endif
7472
}
7573
}

src/NLog.WindowsIdentity/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@
4242
[assembly: ComVisible(false)]
4343
[assembly: InternalsVisibleTo("NLog.WindowsIdentity.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100ef8eab4fbdeb511eeb475e1659fe53f00ec1c1340700f1aa347bf3438455d71993b28b1efbed44c8d97a989e0cb6f01bcb5e78f0b055d311546f63de0a969e04cf04450f43834db9f909e566545a67e42822036860075a1576e90e1c43d43e023a24c22a427f85592ae56cac26f13b7ec2625cbc01f9490d60f16cfbb1bc34d9")]
4444
[assembly: AllowPartiallyTrustedCallers]
45-
#if !NET35 && !NETSTANDARD1_3 && !NETSTANDARD1_5
45+
#if !NET35
4646
[assembly: SecurityRules(SecurityRuleSet.Level1)]
4747
#endif

0 commit comments

Comments
 (0)