WCF Parallel Impersonation

本秂侑毒 提交于 2019-12-07 09:16:25

问题


I have a WCF service with "ImpersonationOption.Required". The impersonation does not seem to flow through when using parallelism. For example:

Parallel.ForEach(items => results.Add(SystemUtil.WindowsUser.Name)

Will return a number with the impersonated user, and a number with the app pool user. Can impersonation be made to work with parallelism?

Best,

Marc

Update:

This is actual code on the IIS Service side.

[OperationBehavior(Impersonation = ImpersonationOption.Required)]
public string[] WhoAmI(int numOfTests)
{
    var tests = new List<int>();
    for (var i = 0; i < numOfTests; i++)
        tests.Add(i);

    var results = new ConcurrentBag<string>();
    Parallel.ForEach(tests, (test) => results.Add(WindowsIdentity.GetCurrent(false).Name));
    return results.ToArray();
}

If I pass in numOfTests = 10, it spawns 10 tasks and return the WindowsIndentity Name of each task. What I get is ~70% "IIS APPPOOL.NET v4.0" and ~30% me.

How can I set it such that my identity always makes it into the Parallel.ForEach?

Thanks!


回答1:


You need to take care of that yourself. Try something like this:

IntPtr token = WindowsIdentity.GetCurrent().Token;

Parallel.ForEach( Enumerable.Range( 1, 100 ), ( test ) =>
{
    using ( WindowsIdentity.Impersonate( token ) )
    {
          Console.WriteLine( WindowsIdentity.GetCurrent( false ).Name );
    }
} );


来源:https://stackoverflow.com/questions/12395819/wcf-parallel-impersonation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!