database - WCF 服务不模拟客户端

标签 database wcf impersonation

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

问题: 我有一个运行在 1 台服务器上的 WCF 服务,它连接到一个单独的服务器以获取它需要检索的必要数据。我的问题是,当从客户端计算机调用服务时,我收到一条数据库 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();
}

最佳答案

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

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

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

关于database - WCF 服务不模拟客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4372606/

相关文章:

.net - InstallShield可以将应用程序添加到Windows防火墙异常(exception)列表吗?

javascript - 发布时更改 oData 条目中的属性。 [WCF 数据服务 + EF + SQL Express]

c# - 如何在不受信任的域中模拟用户?

mysql - 如何对不熟悉的行进行分组?

c# - WCF服务可以返回Zip流吗?

c# - Rhino Mocks 模拟 WindowsImpersonationContext

c# - .Net 中的模拟在读取注册表项时崩溃(LinkLabel SecurityException)

mysql - 如何为大型数据库使用命名约定?

mysql - 有没有一种方法可以将数据从 mySQL 中的一个字段中分离出来?

php - SQL 查询速度和细化