I have a .net web service that runs with windows authentication and impersonation set to true. When the app goes to connect to the database however it needs to stop impersonating and run as the service account for the application pool.
My understanding has always been that the way to stop impersonation I would run the following code.
WindowsImpersonationContext ctx = WindowsIdentity.Impersonate(IntPtr.Zero);
and then when I want to restart impersonation
ctx.Undo();
But for some reason when I run my code it never stops impersonating the calling user but continues as that user and fails to connect to the sql db which is using integrated security and only the service account has db access.
What am I missing here to get this to work properly?