Impersonating user with Entity Framework

前端 未结 1 1711
你的背包
你的背包 2020-12-07 04:59

So we have our web app up and going with entity framework. What we\'d like to do is impersonate the current user when we\'re accessing the DB. We\'re not interested in set

相关标签:
1条回答
  • 2020-12-07 05:21

    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.

    0 讨论(0)
提交回复
热议问题