How do I set the user identity for tasks when calling Task.WaitAll()?

前端 未结 2 1943
南方客
南方客 2020-12-06 12:12

I\'m having a problem invoking WCF services in parallel. I\'m trying to use Tasks to invoke two services in parallel to save some time (I\'m NOT trying to make this async),

相关标签:
2条回答
  • 2020-12-06 13:05

    Try this:

    WindowsIdentity impersonatedUser = WindowsIdentity.GetCurrent();
    
    Task<object>.Factory.StartNew(() =>
    {
        using (WindowsImpersonationContext ctx = impersonatedUser.Impersonate())
        {
           //Your code
        }
        return data;
    });
    
    0 讨论(0)
  • 2020-12-06 13:09

    You might want to look into whether setting <alwaysFlowImpersonationContext> is appropriate:

    Specifies that the Windows identity always flows across asynchronous points, regardless of how impersonation was performed.

    By default, this setting is off within ASP.Net, and so the tasks (running, potentially, on other threads) end up just running with no impersonation in place.

    Otherwise (if it's not appropriate to set that), I think you could capture the WindowsIdentity object into a local variable that's captured by the Tasks, and then call Impersonate() on it from within the task. (although I haven't tried this)

    0 讨论(0)
提交回复
热议问题