Windows Authentication EF Impersonation

http://stackoverflow.com/questions/1954944/impersonating-user-with-entity-framework

 

Your EF connection string is going to need to be set up for using a trusted connection.

You won’t need to set up Impersonation in your web.config, but you do need to be using Windows Authentication.

Then just do this:

using (((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
using (var dbContext = new MyEntityFrameworkContainer())
{
    ...
}

Any code inside the curly braces of the using statements will run as the authenticated user.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s