Bastyon February 2016

How do I get a .net web service call to stop impersonating to perform a task

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?

Answers


Bastyon February 2016

It seems that the method I was using is fine. The error was a problem with the sql connection and the assumption that it was Impersonation that was failing. I was confirming my assumption by using the wrong check to verify the identity. The code in my original question does in fact work.

Post Status

Asked in February 2016
Viewed 1,050 times
Voted 12
Answered 1 times

Search




Leave an answer