diff --git a/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs b/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs
index 5f25c90..6a9f569 100644
--- a/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs
+++ b/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs
@@ -54,7 +54,7 @@ namespace NLog.Targets.Wrappers
public class ImpersonatingTargetWrapper : WrapperTargetBase
{
private NewIdentityHandle _newIdentity;
-
+
///
/// Initializes a new instance of the class.
///
@@ -240,11 +240,11 @@ internal sealed class NewIdentityHandle : IDisposable
public string Domain { get; }
public int Password { get; }
-#if NETSTANDARD
- public Microsoft.Win32.SafeHandles.SafeAccessTokenHandle Handle { get; }
-#else
+#if NETFRAMEWORK
public WindowsIdentity Handle { get; }
private readonly IntPtr _handle = IntPtr.Zero;
+#else
+ public Microsoft.Win32.SafeHandles.SafeAccessTokenHandle Handle { get; }
#endif
public NewIdentityHandle(string userName, string domain, string password, SecurityLogOnType logOnType, LogOnProviderType logOnProvider, SecurityImpersonationLevel impersonationLevel)
@@ -264,9 +264,7 @@ public NewIdentityHandle(string userName, string domain, string password, Securi
throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error());
}
-#if NETSTANDARD
- Handle = logonHandle;
-#else
+#if NETFRAMEWORK
// adapted from:
// https://www.codeproject.com/csharp/cpimpersonation1.asp
if (!NativeMethods.DuplicateToken(logonHandle, (int)impersonationLevel, out _handle))
@@ -279,6 +277,8 @@ public NewIdentityHandle(string userName, string domain, string password, Securi
// create new identity using new primary token)
Handle = new WindowsIdentity(_handle);
+#else
+ Handle = logonHandle;
#endif
}
@@ -290,7 +290,7 @@ public bool IsValid(string userName, string domain, string password)
public void Close()
{
Handle.Dispose();
-#if !NETSTANDARD
+#if NETFRAMEWORK
if (_handle != IntPtr.Zero)
NativeMethods.CloseHandle(_handle);
#endif
@@ -303,9 +303,7 @@ public void Dispose()
internal static void RunImpersonated(NewIdentityHandle newIdentity, Action executeOperation, T state)
{
-#if NETSTANDARD
- WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
-#else
+#if NETFRAMEWORK
WindowsImpersonationContext context = null;
try
{
@@ -316,6 +314,8 @@ internal static void RunImpersonated(NewIdentityHandle newIdentity, Action
{
context?.Undo();
}
+#else
+ WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
#endif
}
}
diff --git a/src/NLog.WindowsIdentity/NLog.WindowsIdentity.csproj b/src/NLog.WindowsIdentity/NLog.WindowsIdentity.csproj
index 8897397..92e7406 100644
--- a/src/NLog.WindowsIdentity/NLog.WindowsIdentity.csproj
+++ b/src/NLog.WindowsIdentity/NLog.WindowsIdentity.csproj
@@ -1,7 +1,7 @@
- net46;net45;net35;netstandard1.5;netstandard2.0
+ net46;net45;net35;netstandard1.5;netstandard2.0;net6.0-windows;net8.0-windows
NLog
5.3.0
@@ -73,6 +73,14 @@ ImpersonatingWrapper Target Docs:
NLog.WindowsIdentity for NetStandard 2.0
+
+ NLog.WindowsIdentity for .Net 6.0
+
+
+
+ NLog.WindowsIdentity for .Net 8.0
+
+
diff --git a/src/NLog.WindowsIdentity/NativeMethods.cs b/src/NLog.WindowsIdentity/NativeMethods.cs
index f5dc6c6..df24bcb 100644
--- a/src/NLog.WindowsIdentity/NativeMethods.cs
+++ b/src/NLog.WindowsIdentity/NativeMethods.cs
@@ -38,7 +38,7 @@ namespace NLog.Internal
internal static class NativeMethods
{
-#if NETSTANDARD
+#if !NETFRAMEWORK
// obtains user token
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
#if !NET35
diff --git a/tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs b/tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs
index 664c12f..1dd8313 100644
--- a/tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs
+++ b/tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs
@@ -55,7 +55,7 @@ public ImpersonatingTargetWrapperTests()
LogManager.ThrowExceptions = true;
}
-#if !NETSTANDARD
+#if NETFRAMEWORK
[Fact]
#else
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
@@ -100,7 +100,7 @@ public void ImpersonatingWrapperTest()
logFactory.Shutdown();
}
-#if !NETSTANDARD
+#if NETFRAMEWORK
[Fact]
#else
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
@@ -207,7 +207,7 @@ public void RevertToSameIdentity()
logFactory.Shutdown();
}
-#if !NETSTANDARD
+#if NETFRAMEWORK
[Fact]
#else
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
@@ -242,7 +242,7 @@ public void ImpersonatingWrapperNegativeTest()
logFactory.Shutdown(); // will not fail because Initialize() failed
}
-#if !NETSTANDARD
+#if NETFRAMEWORK
[Fact]
#else
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
@@ -338,7 +338,7 @@ private void CreateUserIfNotPresent()
return;
}
-#if !NETSTANDARD
+#if NETFRAMEWORK
var user = new UserPrincipal(context);
user.SetPassword(NLogTestUserPassword);
user.Name = NLogTestUser;
diff --git a/tests/NLog.WindowsIdentity.Tests/NLog.WindowsIdentity.Tests.csproj b/tests/NLog.WindowsIdentity.Tests/NLog.WindowsIdentity.Tests.csproj
index d651992..f43376d 100644
--- a/tests/NLog.WindowsIdentity.Tests/NLog.WindowsIdentity.Tests.csproj
+++ b/tests/NLog.WindowsIdentity.Tests/NLog.WindowsIdentity.Tests.csproj
@@ -1,7 +1,7 @@
- net452;net461;netcoreapp3.1
+ net452;net461;netcoreapp3.1;net6.0-windows;net8.0-windows
false
../NLogTests.snk
@@ -20,14 +20,14 @@
-
+
-
-
+
+
-
+