异步/等待与实体框架6.1.1和模拟实体、框架

2023-09-03 16:15:18 作者:北笙

我在IIS中承载的是从多个来源(所有SQL Server)恢复数据的WCF服务。对于每个数据源,我也冒充不同的Active Directory用户连接到数据库。我使用实体框架V6.1.1两个数据源。集成安全性设置为True,在连接字符串了。

I have a WCF service hosted in IIS that is retrieving data from multiple sources (all SQL Server). With each data source, I have to impersonate a different Active Directory user to connect to the database. I am using Entity Framework v6.1.1 for two of the data sources. Integrated Security is set to True in the connection strings, too.

我用以下的例子来设置模拟的用户,在模拟的用户是 System.Security.Principal.WindowsImpersonationContext ,我从配置设置:

I use the example below to set the impersonated user, where the impersonated user is a System.Security.Principal.WindowsImpersonationContext that I set from configuration:

internal async Task<List<string>> GetItemsByLookupItemsAsync(List<string> lookupItems) 
{
    var result = new List<string>();

    using (var db = new EntityFrameworkDb()) 
    {

        var query = from item in db.Table
                    where lookupItems.Contains(item.LookupColumn)
                    select item.StringColumn;

        var queryResult = new List<string>();
        using (GetImpersonatedUser())
        {
            queryResult.AddRange(await query.ToListAsync());
        }

        result.AddRange(queryResult.OrderBy(e => e));
    }

    return result;
}

该问题是previous code抛出一个 SQLEXCEPTION 说,运行Web服务的账号不能登录到数据库。看来,当我打的等待我失去了模拟上下文。

The problem is that the previous code throws a SqlException saying that the account running the web service can not log on to the database. It appears that when I hit the await I lose the impersonation context.

什么是一些建议来解决这个问题?

What are some suggestions to solve this problem?

推荐答案

将legacyImpersonationPolicy以和alwaysFlowImpersonationPolicy以里面的的web.config 键,重新启动IIS

Set the legacyImpersonationPolicy to false and alwaysFlowImpersonationPolicy to true inside your web.config and restart IIS

<configuration>
   <runtime>
     <legacyImpersonationPolicy enabled="false"/>
    <alwaysFlowImpersonationPolicy enabled="true"/>   
  </runtime>
</configuration>
 
精彩推荐
图片推荐