问题描述
所以我们通过实体框架来完成我们的Web应用程序。当我们访问数据库时,我们想要做的是模拟当前用户。我们不想在我们的网络配置中设置模拟。
理想地使用这样的东西:当我们要访问数据。
UPDATED:我正在寻找一种抽象代码的方式,所以我不必在每个存储库函数调用中都有它。
您的EF连接字符串将需要设置为使用受信任的连接。
您不需要设置模拟在您的web.config中,但您确实需要使用Windows身份验证。
然后只需执行此操作:
<$使用(((WindowsIdentity)HttpContext.Current.User.Identity).Impersonate())
using(var dbContext = new MyEntityFrameworkContainer())
{
...
}
使用语句的大括号内的任何代码将运行已验证用户。
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 setting impersonation up in our web config.
Ideally using something like this: http://geekswithblogs.net/jkurtz/archive/2007/08/27/114992.aspx when we're about to access data.
UPDATED: I'm looking for a way to abstract this code out so I don't have to have it in every repository function call.
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.
这篇关于用Entity Framework模拟用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!