为什么我会在WCF远程与本地运行的wmi查询中获得不同的结果?

时间:2011-11-01 16:59:35

标签: c# wcf wmi

我在WCF服务中通过csharp / .net查询打印机。在本地调用时(即从本地计算机运行的客户端)它返回一组打印机。当远程调用时,它会调用另一组。

wcf服务设置为使用在创建客户端时传递的凭据接受和模拟。

我通过远程调试注意到的主要区别是调用中的身份验证类型:

WindowsIdentity.GetCurrent()

远程调用时为Kerberos,本地调用时为Neogotiate。

以下是代码的快速抽样:

[OperationBehavior(Impersonation = ImpersonationOption.Required)]
    public List<string> GetAvailablePrinters()
    {
        List<string> retval = new List<string>();

        using (ManagementClass printerClass = new ManagementClass("win32_printer"))
        {
            ManagementObjectCollection printers = printerClass.GetInstances();
            foreach (ManagementObject printer in printers)
            {
                if ((bool)printer["Shared"] == true)
                    retval.Add((string)printer["Name"]);
            }
        }

        return retval;
    }

两次调用都是成功的,但是我在本地得到了正确的列表,没有任何远程列表。

这是两者的并排:

在本地服务器上运行Test Executable:

{System.Security.Principal.WindowsIdentity}
AuthenticationType: "Negotiate"
Groups: {System.Security.Principal.IdentityReferenceCollection}
ImpersonationLevel: Impersonation
IsAnonymous: false
IsAuthenticated: true
IsGuest: false
IsSystem: false
m_authType: null
m_groups: {System.Security.Principal.IdentityReferenceCollection}
m_impersonationLevel: Impersonation
m_isAuthenticated: 1
m_name: null
m_owner: null
m_safeTokenHandle: {Microsoft.Win32.SafeHandles.SafeTokenHandle}
m_user: {xxxxx}
Name: "adomain\\auser"
Owner: {xxxxx}
Token: token number
TokenHandle: {Microsoft.Win32.SafeHandles.SafeTokenHandle}
User: {xxxxxxxx}

相同的可执行文件远程运行

{System.Security.Principal.WindowsIdentity}
AuthenticationType: "Kerberos"
Groups: {System.Security.Principal.IdentityReferenceCollection}
ImpersonationLevel: Impersonation
IsAnonymous: false
IsAuthenticated: true
IsGuest: false
IsSystem: false
m_authType: null
m_groups: {System.Security.Principal.IdentityReferenceCollection}
m_impersonationLevel: Impersonation
m_isAuthenticated: 1
m_name: null
m_owner: null
m_safeTokenHandle: {Microsoft.Win32.SafeHandles.SafeTokenHandle}
m_user: {xxxxx}
Name: "adomain\\auser"
Owner: {differnt owner}
Token: different Token number
TokenHandle: {Microsoft.Win32.SafeHandles.SafeTokenHandle}
User: {xxxxxx}

1 个答案:

答案 0 :(得分:2)

您尝试列出的打印机似乎是 远程打印机 到运行WCF服务的计算机。模拟只允许您访问被调用计算机上可用的本地资源。这篇关于impersonation and delegation in WCF的MSDN文章应该会让你走在正确的轨道上。您需要实现委派以从WCF服务列出远程资源,或者根本不使用委托,并使WCF服务在可以列出远程打印机的域帐户下运行。