I’m incorporating Entity Framework and ASP.NET Dynamic Data into an existing application that is setup to use impersonation=”true” in the web.config, however the previous developers chose an approach where they revert back to the app pool identity for each DAL call they made
private WindowsImpersonationContext context = null;
public void RevertToAppPool()
{
if (!WindowsIdentity.GetCurrent().IsSystem)
{
context = WindowsIdentity.Impersonate(System.IntPtr.Zero);
}
}
public void UndoImpersonation()
{
if (context != null)
{
context.Undo();
}
}
I’ve been asked to preserve this behavior while using Entity Framework …given that the entity objects are used all over the place (in LINQ to EF queries, manual calls, behind-the-scenes calls by the framework etc.), where would be the appropriate places in the partial classes to make the appropriate calls to RevertToAppPool and UndoImpersonation for every call?
You would most probably need to create custom Entity framework provider wrapper and in wrapped connection revert impersonation before connection opening and put it back after connection has been openned (hopefully it will be enough). You will have much easier live if you use SQL authentication for database instead.
Imperesonation with reverting back for data access looks like pretty odd solution. I wonder what is the point of impersonation in that application?