Skip to content

Commit cd95641

Browse files
authored
Merge pull request #179 from i-am-not-an-ai/master
Impersonation support
2 parents 116da3b + e09a941 commit cd95641

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

SnaffCore/Concurrency/BlockingTaskScheduler.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Numerics;
4+
using System.Security.Principal;
45
using System.Threading;
56
using System.Threading.Tasks;
67

@@ -62,7 +63,13 @@ public void New(Action action)
6263
// okay, let's add the thing
6364
proceed = true;
6465

65-
_taskFactory.StartNew(action, _cancellationSource.Token);
66+
WindowsIdentity impersonatedUser = WindowsIdentity.GetCurrent();
67+
_taskFactory.StartNew(() => {
68+
using (WindowsImpersonationContext ctx = impersonatedUser.Impersonate())
69+
{
70+
action();
71+
}
72+
}, _cancellationSource.Token);
6673
}
6774
}
6875
}

Snaffler/SnaffleRunner.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NLog;
1+
using NLog;
22
using NLog.Config;
33
using NLog.Layouts;
44
using NLog.Targets;
@@ -8,9 +8,10 @@
88
using System;
99
using System.Diagnostics;
1010
using System.IO;
11-
using System.Threading.Tasks;
11+
using System.Security.Principal;
1212
using System.Text.RegularExpressions;
1313
using System.Threading;
14+
using System.Threading.Tasks;
1415

1516
namespace Snaffler
1617
{
@@ -201,7 +202,14 @@ public void Run(string[] args)
201202

202203
var tokenSource = new CancellationTokenSource();
203204
var token = tokenSource.Token;
204-
Task thing = Task.Factory.StartNew(() => { controller.Execute(); }, token);
205+
206+
WindowsIdentity impersonatedUser = WindowsIdentity.GetCurrent();
207+
Task thing = Task.Factory.StartNew(() => {
208+
using (WindowsImpersonationContext ctx = impersonatedUser.Impersonate())
209+
{
210+
controller.Execute();
211+
}
212+
}, token);
205213
bool exit = false;
206214

207215
while (exit == false)

0 commit comments

Comments
 (0)