EF + UnitOfWork + SharePoint RunWithElevatedPrivileges

Posted by Lorenzo on Stack Overflow See other posts from Stack Overflow or by Lorenzo
Published on 2011-11-30T14:30:42Z Indexed on 2012/10/18 23:01 UTC
Read the original article Hit count: 361

In our SharePoint application we have used the UnitOfWork + Repository patterns together with Entity Framework. To avoid the usage of the passthrough authentication we have developed a piece of code that impersonate a single user before creating the ObjectContext instance in a similar way that is described in "Impersonating user with Entity Framework" on this site.

The only difference between our code and the referred question is that, to do the impersonation, we are using RunWithElevatedPrivileges to impersonate the Application Pool identity as in the following sample.

SPSecurity.RunWithElevatedPrivileges(delegate() { 
    using (SPSite site = new SPSite(url)) {
        _context = new MyDataContext(ConfigSingleton.GetInstance().ConnectionString);
    }
});

We have done this way because we expected that creating the ObjectContext after impersonation and, due to the fact that Repositories are receiving the impersonated ObjectContext would solve our requirement.

Unfortunately it's not so easy. In fact we experienced that, even if the ObjectContext is created before and under impersonation circumstances, the real connection is made just before executing the query, and so does not use impersonation, which break our requirement.

I have checked the ObjectContext class to see if there was any event through which we can inject the impersonation but unfortunately found nothing.

Any help?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about sharepoint