Concurrent Impersonation of WCF

I have a WCF service with "ImersonationOption.Required". When using parallelism, impersonation does not seem to leak. For instance:

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

Will return a number with an impersonated user and a number with an application pool user. Can I get myself to work with parallelism?

Best

Mark

Update:

This is the actual code on the IIS 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 at numOfTests = 10, it spawns 10 tasks and returns the WindowsIndentity name of each task. What I get is ~ 70% "IIS APPPOOL.NET v4.0" and ~ 30% me.

How can I set it in such a way that my personality always turns it into Parallel.ForEach?

Thanks!

+4
source share
1 answer

You need to take care of this 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 ); } } ); 
+2
source

Source: https://habr.com/ru/post/1433890/


All Articles