Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9 add in net 6 #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
//
//
// Copyright (c) 2004-2021 Jaroslaw Kowalski <[email protected]>, 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
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
// 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
{
Expand All @@ -54,7 +54,7 @@ namespace NLog.Targets.Wrappers
public class ImpersonatingTargetWrapper : WrapperTargetBase
{
private NewIdentityHandle _newIdentity;

/// <summary>
/// Initializes a new instance of the <see cref="ImpersonatingTargetWrapper" /> class.
/// </summary>
Expand Down Expand Up @@ -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
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -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
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
// adapted from:
// https://www.codeproject.com/csharp/cpimpersonation1.asp
if (!NativeMethods.DuplicateToken(logonHandle, (int)impersonationLevel, out _handle))
Expand All @@ -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
}

Expand All @@ -290,7 +290,7 @@ public bool IsValid(string userName, string domain, string password)
public void Close()
{
Handle.Dispose();
#if !NETSTANDARD
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
if (_handle != IntPtr.Zero)
NativeMethods.CloseHandle(_handle);
#endif
Expand All @@ -303,9 +303,7 @@ public void Dispose()

internal static void RunImpersonated<T>(NewIdentityHandle newIdentity, Action<T> executeOperation, T state)
{
#if NETSTANDARD
WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
#else
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
WindowsImpersonationContext context = null;
try
{
Expand All @@ -316,6 +314,8 @@ internal static void RunImpersonated<T>(NewIdentityHandle newIdentity, Action<T>
{
context?.Undo();
}
#else
WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
#endif
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/NLog.WindowsIdentity/NLog.WindowsIdentity.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' ">net46;net45;net35;netstandard1.5;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(TargetFrameworks)' == '' ">net46;net45;net35;netstandard1.5;netstandard2.0;net6.0-Windows</TargetFrameworks>
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
<RootNamespace>NLog</RootNamespace>

<VersionPrefix>5.3.0</VersionPrefix>
Expand Down Expand Up @@ -73,6 +73,10 @@ ImpersonatingWrapper Target Docs:
<Title>NLog.WindowsIdentity for NetStandard 2.0</Title>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
<Title>NLog.WindowsIdentity for Net 6.0</Title>
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' or '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net35' ">
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
48 changes: 24 additions & 24 deletions src/NLog.WindowsIdentity/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
//
//
// Copyright (c) 2004-2021 Jaroslaw Kowalski <[email protected]>, 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.
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
//
// * 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
{
Expand All @@ -38,7 +38,7 @@ namespace NLog.Internal

internal static class NativeMethods
{
#if NETSTANDARD
#if !NET35 && !NET45 && !NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
// obtains user token
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
#if !NET35
Expand Down
72 changes: 36 additions & 36 deletions tests/NLog.WindowsIdentity.Tests/ImpersonatingTargetWrapperTests.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
//
//
// Copyright (c) 2004-2021 Jaroslaw Kowalski <[email protected]>, 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,
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
// 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
{
Expand All @@ -55,10 +55,10 @@ public ImpersonatingTargetWrapperTests()
LogManager.ThrowExceptions = true;
}

#if !NETSTANDARD
[Fact]
#else
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
#else
[Fact]
#endif
public void ImpersonatingWrapperTest()
{
Expand Down Expand Up @@ -100,10 +100,10 @@ public void ImpersonatingWrapperTest()
logFactory.Shutdown();
}

#if !NETSTANDARD
[Fact]
#else
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
#else
[Fact]
#endif
public void RevertToSelfTest()
{
Expand Down Expand Up @@ -207,10 +207,10 @@ public void RevertToSameIdentity()
logFactory.Shutdown();
}

#if !NETSTANDARD
[Fact]
#else
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
#else
[Fact]
#endif
public void ImpersonatingWrapperNegativeTest()
{
Expand Down Expand Up @@ -242,10 +242,10 @@ public void ImpersonatingWrapperNegativeTest()
logFactory.Shutdown(); // will not fail because Initialize() failed
}

#if !NETSTANDARD
[Fact]
#else
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
[Fact(Skip = "CreateUserIfNotPresent fails with NetCore")]
#else
[Fact]
#endif
public void ImpersonatingWrapperNegativeTest2()
{
Expand Down Expand Up @@ -338,7 +338,7 @@ private void CreateUserIfNotPresent()
return;
}

#if !NETSTANDARD
#if NET35 || NET45 || NET46
thompson-tomo marked this conversation as resolved.
Show resolved Hide resolved
var user = new UserPrincipal(context);
user.SetPassword(NLogTestUserPassword);
user.Name = NLogTestUser;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;net461;netcoreapp3.1</TargetFrameworks>
<TargetFrameworks>net452;net461;netcoreapp3.1;net6.0</TargetFrameworks>
<IsPackable>false</IsPackable>

<AssemblyOriginatorKeyFile>../NLogTests.snk</AssemblyOriginatorKeyFile>
Expand All @@ -20,14 +20,14 @@
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' != 'netcoreapp3.1' ">
<ItemGroup Condition=" '$(TargetFramework)' == 'net452' or '$(TargetFramework)' == 'net461' ">
<Reference Include="System.DirectoryServices.AccountManagement" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">

<ItemGroup Condition=" '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net461' ">
<PackageReference Include="System.DirectoryServices.AccountManagement" Version="5.0.0" />
<PackageReference Include="System.DirectoryServices.Protocols" Version="5.0.1" />
</ItemGroup>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\NLog.WindowsIdentity\NLog.WindowsIdentity.csproj" />
Expand Down