// naive task code
Task.Run(() =>
{
// Task code here
});
To make this work, I need to invoke the method in the task under impersonated context. The easiest way is to capture the WindowsIdentity
and make it available to the task function so it can execute any code under the impersonated context.
// Task using closure
var currentWindowsIdentity = WindowsIdentity.GetCurrent();
Task.Run(() =>
{
using (currentWindowsIdentity.Impersonate())
{
// Task code here
}
});