diff --git a/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs b/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs index 5f25c90..cde5149 100644 --- a/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs +++ b/src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs @@ -1,35 +1,35 @@ -// +// // Copyright (c) 2004-2021 Jaroslaw Kowalski , Kim Christensen, Julian Verdurmen -// +// // All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions // are met: -// -// * Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * Neither the name of Jaroslaw Kowalski nor the names of its +// and/or other materials provided with the distribution. +// +// * Neither the name of Jaroslaw Kowalski nor the names of its // contributors may be used to endorse or promote products derived from this -// software without specific prior written permission. -// +// software without specific prior written permission. +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // THE POSSIBILITY OF SUCH DAMAGE. -// +// namespace NLog.Targets.Wrappers { @@ -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 NET35 || NET45 || NET46 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 NET35 || NET45 || NET46 // 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 NET35 || NET45 || NET46 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 NET35 || NET45 || NET46 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 83b15a3..a8d3deb 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;net6.0 + net46;net45;net35;netstandard1.5;netstandard2.0;net6.0-Windows NLog 5.3.0 @@ -73,6 +73,10 @@ ImpersonatingWrapper Target Docs: NLog.WindowsIdentity for NetStandard 2.0 + + NLog.WindowsIdentity for Net 6.0 + + diff --git a/src/NLog.WindowsIdentity/NativeMethods.cs b/src/NLog.WindowsIdentity/NativeMethods.cs index f5dc6c6..d768e04 100644 --- a/src/NLog.WindowsIdentity/NativeMethods.cs +++ b/src/NLog.WindowsIdentity/NativeMethods.cs @@ -1,35 +1,35 @@ -// +// // Copyright (c) 2004-2021 Jaroslaw Kowalski , Kim Christensen, Julian Verdurmen -// +// // All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions // are met: -// -// * Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * Neither the name of Jaroslaw Kowalski nor the names of its +// and/or other materials provided with the distribution. +// +// * Neither the name of Jaroslaw Kowalski nor the names of its // contributors may be used to endorse or promote products derived from this -// software without specific prior written permission. -// +// software without specific prior written permission. +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // THE POSSIBILITY OF SUCH DAMAGE. -// +// namespace NLog.Internal { @@ -38,7 +38,7 @@ namespace NLog.Internal internal static class NativeMethods { -#if NETSTANDARD +#if !NET35 && !NET45 && !NET46 // 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..89e7ea7 100644 --- a/tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs +++ b/tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs @@ -1,35 +1,35 @@ -// +// // Copyright (c) 2004-2021 Jaroslaw Kowalski , Kim Christensen, Julian Verdurmen -// +// // All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions // are met: -// -// * Redistributions of source code must retain the above copyright notice, -// this list of conditions and the following disclaimer. -// +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// // * Redistributions in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation -// and/or other materials provided with the distribution. -// -// * Neither the name of Jaroslaw Kowalski nor the names of its +// and/or other materials provided with the distribution. +// +// * Neither the name of Jaroslaw Kowalski nor the names of its // contributors may be used to endorse or promote products derived from this -// software without specific prior written permission. -// +// software without specific prior written permission. +// // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE -// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE -// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF -// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN -// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) -// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF // THE POSSIBILITY OF SUCH DAMAGE. -// +// namespace NLog.WindowsIdentity.Tests { @@ -55,10 +55,10 @@ public ImpersonatingTargetWrapperTests() LogManager.ThrowExceptions = true; } -#if !NETSTANDARD - [Fact] -#else +#if NET35 || NET45 || NET46 [Fact(Skip = "CreateUserIfNotPresent fails with NetCore")] +#else + [Fact] #endif public void ImpersonatingWrapperTest() { @@ -100,10 +100,10 @@ public void ImpersonatingWrapperTest() logFactory.Shutdown(); } -#if !NETSTANDARD - [Fact] -#else +#if NET35 || NET45 || NET46 [Fact(Skip = "CreateUserIfNotPresent fails with NetCore")] +#else + [Fact] #endif public void RevertToSelfTest() { @@ -207,10 +207,10 @@ public void RevertToSameIdentity() logFactory.Shutdown(); } -#if !NETSTANDARD - [Fact] -#else +#if NET35 || NET45 || NET46 [Fact(Skip = "CreateUserIfNotPresent fails with NetCore")] +#else + [Fact] #endif public void ImpersonatingWrapperNegativeTest() { @@ -242,10 +242,10 @@ public void ImpersonatingWrapperNegativeTest() logFactory.Shutdown(); // will not fail because Initialize() failed } -#if !NETSTANDARD - [Fact] -#else +#if NET35 || NET45 || NET46 [Fact(Skip = "CreateUserIfNotPresent fails with NetCore")] +#else + [Fact] #endif public void ImpersonatingWrapperNegativeTest2() { @@ -338,7 +338,7 @@ private void CreateUserIfNotPresent() return; } -#if !NETSTANDARD +#if NET35 || NET45 || NET46 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..2fcfce0 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 false ../NLogTests.snk @@ -20,14 +20,14 @@ - + - - + + - +