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
Changes from 5 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
22 changes: 11 additions & 11 deletions src/NLog.WindowsIdentity/ImpersonatingTargetWrapper.cs
Original file line number Diff line number Diff line change
@@ -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>
@@ -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<T>(NewIdentityHandle newIdentity, Action<T> 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<T>(NewIdentityHandle newIdentity, Action<T>
{
context?.Undo();
}
#else
WindowsIdentity.RunImpersonated(newIdentity?.Handle ?? Microsoft.Win32.SafeHandles.SafeAccessTokenHandle.InvalidHandle, () => executeOperation.Invoke(state));
#endif
}
}
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>
<RootNamespace>NLog</RootNamespace>

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

<PropertyGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<Title>NLog.WindowsIdentity for Net 6.0</Title>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' or '$(TargetFramework)' == 'net45' or '$(TargetFramework)' == 'net35' ">
<Reference Include="System" />
<Reference Include="System.Core" />
2 changes: 1 addition & 1 deletion src/NLog.WindowsIdentity/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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;
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-windows</TargetFrameworks>
<IsPackable>false</IsPackable>

<AssemblyOriginatorKeyFile>../NLogTests.snk</AssemblyOriginatorKeyFile>
@@ -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" />