检查代码是否在WCF中以模拟Id运行

检查代码是否在WCF中以模拟Id运行,wcf,iis-7,impersonation,Wcf,Iis 7,Impersonation,我们有一个WCF服务托管在IIS中,在“网络服务”帐户下的默认AppPool下运行。在服务中的某个位置,需要从网络服务帐户无权访问的位置读取文件,因此我们需要使用“管理员”帐户将该文件加载到内存中 我已经尝试了模拟来完成这项工作,我的代码没有为我传入的管理员凭据引发任何异常,我确信这些凭据是正确的。但WindowsIdentity.GetCurrent.Name即使在调用模拟后仍显示NT Authority\Network服务。我正在使用LogonUser Win32调用,许多人根据我的搜索使用

我们有一个WCF服务托管在IIS中,在“网络服务”帐户下的默认AppPool下运行。在服务中的某个位置,需要从网络服务帐户无权访问的位置读取文件,因此我们需要使用“管理员”帐户将该文件加载到内存中

我已经尝试了模拟来完成这项工作,我的代码没有为我传入的管理员凭据引发任何异常,我确信这些凭据是正确的。但WindowsIdentity.GetCurrent.Name即使在调用模拟后仍显示NT Authority\Network服务。我正在使用LogonUser Win32调用,许多人根据我的搜索使用该调用。据我所知,我的web.config中没有任何模拟设置

我的问题是WindowsIdentity.GetCurrent.Name是否是检查代码运行时使用的凭据的正确方法。如果这是真的,那么我做错了什么

我尝试了两种不同的模拟方式: 它基本上是win32 LogonUser方法的包装器

我还尝试以本机方式添加代码,如下所示:

/** WindowsIdentity.GetCurrent().Name prints 'NT Authority\Network Service' **/
                    SafeTokenHandle safeTokenHandle;

                    const int logon32ProviderDefault = 0;
                    const int logon32LogonNewCredentials = 9;    // this logon type is required to allow cross-domain (WORKGROUP -> ourDomain) authentication

                    // Call LogonUser to obtain a handle to an access token.
                    bool logonSucceeded = LogonUser("user", "domain", "password", logon32LogonNewCredentials, logon32ProviderDefault, out safeTokenHandle);

                    if (!logonSucceeded)
                    {
                        int ret = Marshal.GetLastWin32Error();
                        throw new System.ComponentModel.Win32Exception(ret);
                    }

     using (safeTokenHandle)
                {
                    WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
                    using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                    {
/** At this point, WindowsIdentity.GetCurrent().Name STILL says NT Authority\Network Service. Shouldn't it say domain\username passed in the call to LogonUser above**/
                    }
                }
在这两种方式中,我都没有得到凭据的异常,因此模拟似乎成功了,但似乎并没有真正将凭据更改为模拟用户