【问题标题】:Impersonation error when calling wcf-service调用 wcf-service 时出现模拟错误
【发布时间】:2011-07-13 09:40:29
【问题描述】:

我开发了一个 wcf 服务。由于它也会被非.net 客户端调用,所以我使用了 basichttpbinding。有些方法需要模拟。这是通过用以下方式装饰网络方法来强制实现的:

 [OperationBehavior(Impersonation = ImpersonationOption.Required)]

在我们的测试服务器上部署服务后,调用服务时出现奇怪的错误:

无法加载文件或程序集“log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821”或其依赖项之一。未提供所需的模拟级别,或者提供的模拟级别无效。 (HRESULT 异常:0x80070542)

我收到此错误的方式与调用服务的方式无关。当我通过 wcfTestClient 调用它时我得到它,当我通过我编写的控制台应用程序调用它时我得到它。 (我将 Web 服务作为 Web 引用添加到此应用程序中,以模拟非 .net 客户端的行为。)

有什么想法吗?


PS:这是我的网络服务的 web.config:

  <system.web>
    <compilation targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding closeTimeout="00:15:00" openTimeout="00:15:00" sendTimeout="00:15:00" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <security mode="TransportCredentialOnly">
            <transport clientCredentialType="Windows" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <defaultDocument>
            <files>
                <add value="CrmConnectorDiamondData.svc" />
            </files>
        </defaultDocument>
  </system.webServer>
</configuration>

【问题讨论】:

    标签: wcf


    【解决方案1】:

    WCF 客户端必须明确允许模拟。在 WCF 客户端中,它是通过配置向客户端代理添加行为来完成的:

    <behaviors>
      <endpointBehaviors>
        <behavior name="myBehavior">
          <clientCredentials>
            <windows allowedImpersonationLevel="Impersonation" />
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    

    或者在代码中:

    proxy.ClientCredentials.Windows.AllowImpersonationLevel = TokenImpersonationLevel.Impersonation;
    

    我希望这必须为 WcfTestClient 配置,因为默认模拟级别只允许识别。

    如果是 ASMX 代理,请确保您是 passing your credentials

    我的观点是,Windows 身份验证对于非 .NET 客户端使用的服务不是一个好的选择(尤其是如果您还指非 Windows)。

    【讨论】:

    • 这有帮助。在我将此行为添加到 wcftestclient 中的 client.config 之后,它就起作用了。但现在我有另一个奇怪的行为。当我通过 wcftestclient 调用服务时(不允许显式模拟),它会因上述错误而崩溃。从那一刻起,当通过控制台应用程序调用它时它也会崩溃,直到我再次重新启动服务......这怎么可能?
    【解决方案2】:

    看起来 log4net 库与该模拟级别不兼容。如果您删除引用,它将起作用。

    【讨论】:

    • 但我需要那个程序集。我必须记录服务器上发生的事情,而 log4net 是“我们的”库。
    • 如果您需要使用该程序集,那么我建议您使用另一种身份验证方法。我个人喜欢使用基于 https 的基本身份验证。
    猜你喜欢
    • 1970-01-01
    • 2014-09-17
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 2015-11-11
    • 1970-01-01
    相关资源
    最近更新 更多