public void RevertToSelfTest()
        {
            var wrapped = new MyTarget()
            {
                ExpectedUser = Environment.UserDomainName + "\\" + Environment.UserName,
            };

            WindowsIdentity originalIdentity = WindowsIdentity.GetCurrent();

            try
            {
                var id = this.CreateWindowsIdentity(NLogTestUser, Environment.MachineName, NLogTestUserPassword, SecurityLogOnType.Interactive, LogOnProviderType.Default, SecurityImpersonationLevel.Identification);
                id.Impersonate();

                WindowsIdentity changedIdentity = WindowsIdentity.GetCurrent();
                Assert.Contains(NLogTestUser.ToLowerInvariant(), changedIdentity.Name.ToLowerInvariant(), StringComparison.InvariantCulture);

                var wrapper = new ImpersonatingTargetWrapper()
                {
                    WrappedTarget = wrapped,
                    RevertToSelf  = true,
                };

                // wrapped.Initialize(null);
                wrapper.Initialize(null);

                var exceptions = new List <Exception>();
                wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
                Assert.Equal(1, exceptions.Count);
                wrapper.WriteAsyncLogEvents(
                    LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                    LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                    LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
                Assert.Equal(4, exceptions.Count);
                wrapper.Flush(exceptions.Add);
                Assert.Equal(5, exceptions.Count);
                foreach (var ex in exceptions)
                {
                    Assert.Null(ex);
                }

                wrapper.Close();
            }
            finally
            {
                // revert to self
                NativeMethods.RevertToSelf();

                WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                Assert.Equal(originalIdentity.Name.ToLowerInvariant(), currentIdentity.Name.ToLowerInvariant());
            }
        }
        public void ImpersonatingWrapperTest()
        {
            CreateUserIfNotPresent();

            var wrapped = new MyTarget()
            {
                ExpectedUser = LocalMachineName + "\\" + NLogTestUser,
            };

            var wrapper = new ImpersonatingTargetWrapper()
            {
                UserName      = NLogTestUser,
                Password      = NLogTestUserPassword,
                Domain        = LocalMachineName,
                WrappedTarget = wrapped,
            };

            var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
            {
                cfg.Configuration.AddRuleForAllLevels(wrapper);
            }).LogFactory;

            var exceptions = new List <Exception>();

            wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Single(exceptions);
            wrapper.WriteAsyncLogEvents(
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Equal(4, exceptions.Count);
            wrapper.Flush(exceptions.Add);
            Assert.Equal(5, exceptions.Count);
            foreach (var ex in exceptions)
            {
                Assert.Null(ex);
            }

            logFactory.Shutdown();
        }
        public void ImpersonatingWrapperTest()
        {
            var wrapped = new MyTarget()
            {
                ExpectedUser = Environment.MachineName + "\\" + NLogTestUser,
            };

            var wrapper = new ImpersonatingTargetWrapper()
            {
                UserName      = NLogTestUser,
                Password      = NLogTestUserPassword,
                Domain        = Environment.MachineName,
                WrappedTarget = wrapped,
            };

            // wrapped.Initialize(null);
            wrapper.Initialize(null);

            var exceptions = new List <Exception>();

            wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Equal(1, exceptions.Count);
            wrapper.WriteAsyncLogEvents(
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Equal(4, exceptions.Count);
            wrapper.Flush(exceptions.Add);
            Assert.Equal(5, exceptions.Count);
            foreach (var ex in exceptions)
            {
                Assert.Null(ex);
            }

            wrapper.Close();
        }
        public void RevertToSameIdentity()
        {
            var wrapped = new MyTarget()
            {
                ExpectedUser = LocalMachineName + "\\" + Environment.UserName,
            };

            var wrapper = new ImpersonatingTargetWrapper()
            {
                WrappedTarget = wrapped,
                RevertToSelf  = true,
            };

            var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
            {
                cfg.Configuration.AddRuleForAllLevels(wrapper);
            }).LogFactory;

            var exceptions = new List <Exception>();

            wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Single(exceptions);
            wrapper.WriteAsyncLogEvents(
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
            Assert.Equal(4, exceptions.Count);
            wrapper.Flush(exceptions.Add);
            Assert.Equal(5, exceptions.Count);
            foreach (var ex in exceptions)
            {
                Assert.Null(ex);
            }

            logFactory.Shutdown();
        }
        public void RevertToSelfTest()
        {
            CreateUserIfNotPresent();

            var wrapped = new MyTarget()
            {
                ExpectedUser = LocalMachineName + "\\" + Environment.UserName,
            };

            WindowsIdentity originalIdentity = WindowsIdentity.GetCurrent();

            var newIdentity = new ImpersonatingTargetWrapper.NewIdentityHandle(
                NLogTestUser,
                LocalMachineName,
                NLogTestUserPassword,
                SecurityLogOnType.Interactive,
                LogOnProviderType.Default,
                SecurityImpersonationLevel.Identification
                );

            try
            {
                ImpersonatingTargetWrapper.NewIdentityHandle.RunImpersonated(newIdentity, (s) =>
                {
                    WindowsIdentity changedIdentity = WindowsIdentity.GetCurrent();
                    Assert.Contains(NLogTestUser.ToLowerInvariant(), changedIdentity.Name.ToLowerInvariant(), StringComparison.InvariantCulture);

                    var wrapper = new ImpersonatingTargetWrapper()
                    {
                        WrappedTarget = wrapped,
                        RevertToSelf  = true,
                    };

                    var logFactory = new LogFactory().Setup().LoadConfiguration(cfg =>
                    {
                        cfg.Configuration.AddRuleForAllLevels(wrapper);
                    }).LogFactory;

                    var exceptions = new List <Exception>();
                    wrapper.WriteAsyncLogEvent(LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
                    Assert.Single(exceptions);
                    wrapper.WriteAsyncLogEvents(
                        LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                        LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add),
                        LogEventInfo.CreateNullEvent().WithContinuation(exceptions.Add));
                    Assert.Equal(4, exceptions.Count);
                    wrapper.Flush(exceptions.Add);
                    Assert.Equal(5, exceptions.Count);
                    foreach (var ex in exceptions)
                    {
                        Assert.Null(ex);
                    }

                    logFactory.Shutdown();
                }, (object)null);
            }
            finally
            {
                newIdentity.Dispose();

                WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent();
                Assert.Equal(originalIdentity.Name.ToLowerInvariant(), currentIdentity.Name.ToLowerInvariant());
            }
        }