【问题标题】:WCF service not impersonating clientWCF 服务不模拟客户端
【发布时间】:2011-05-21 07:58:18
【问题描述】:

物流: 1 台运行 WCF 服务的服务器。 1 台运行 WCF 服务数据库的服务器。

问题: 我有一个 WCF 服务在 1 个服务器上运行,该服务连接到一个单独的服务器以获取它需要检索的必要数据。我的问题是,从客户端计算机调用服务时,我收到一个数据库 sql 错误,指出“用户 'NT AUTHORITY\ANONYMOUS LOGON' 登录失败”。我相信我已将 WCF 服务设置为使用模拟。

WCF 服务器配置:

<bindings>
  <ws2007HttpBinding>
    <binding maxReceivedMessageSize="214748">
      <security mode="Message">
        <transport clientCredentialType="Windows"
                   proxyCredentialType="Windows" realm="" />
        <message clientCredentialType="Windows" negotiateServiceCredential="true"
                 algorithmSuite="Default" establishSecurityContext="true" />
      </security>
    </binding>
  </ws2007HttpBinding>
</bindings>
<services>
  <service behaviorConfiguration="Host.ServiceBehavior" name="Wcf.MyWebService">
    <endpoint address="" behaviorConfiguration=""
              binding="ws2007HttpBinding" contract="Wcf.MyWebServiceSoap">
      <identity>
        <servicePrincipalName value="ServerMachineName" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="Host.ServiceBehavior">
      <serviceMetadata httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
      <serviceAuthorization impersonateCallerForAllOperations="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

WCF 服务代码:

public class MySebService: MyWebServiceSoap
{
   [OperationBehavior(Impersonation = ImpersonationOption.Required)]
   public string TestWebMethod()
   {
     DbDal dal = CreateDataAccessLayer();

     return dal.GetStringFromDatabase();
   }
}

客户端配置和代码:

我正在以编程方式设置以下配置项:

public void TestWebMethod()
{
  WS2007HttpBinding binding = new WS2007HttpBinding();
  EndpointAddress endpoint = new EndpointAddress("uri");
  ServiceClient client = new ServiceClient(binding, endpoint);
  client.ClientCredentials.Windows.AllowedImpersonationLevel =
                               TokenImpersonationLevel.Impersonation;
  client.ClientCredentials.Windows.AllowNtlm = true;
  string result = client.TestWebMethod();
  client.Close();
}

【问题讨论】:

    标签: database wcf impersonation


    【解决方案1】:

    TokenImpersonationLevel.Impersonation 允许服务访问服务本地的资源,但不允许服务访问外部资源(例如,另一个服务)。

    您必须将允许的模拟级别设置为 TokenImpersonationLevel.Delegation

    client.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Delegation;
    

    【讨论】:

    • 我以为我已经尝试将其更改为委托,但它不起作用。但我会再试一次。服务和客户端的配置项看起来正确吗?
    • 我在服务端添加了一些代码来记录调用方法时进入的模拟级别。我已在客户端上将 AllowedImpersonationLevel 设置为委托,但日志显示它仍设置为模拟。我仍然无法登录数据库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多