【问题标题】:The server has rejected the client credentials, WCF as Windows Service服务器已拒绝客户端凭据,WCF 作为 Windows 服务
【发布时间】:2012-02-06 01:03:25
【问题描述】:

我可以使用 Win-form 应用程序连接到我的 WCF 服务,但是我无法使用我的 Windows 服务来连接。每当我向代理触发 open() 时,它都会引发以下错误

服务器拒绝了客户端凭据

内部异常:System.Security.Authentication.InvalidCredentialException:服务器 已拒绝客户端凭据。
---> System.ComponentModel.Win32Exception:登录尝试失败
--- 内部异常堆栈跟踪结束 ---
在 System.Net.Security.NegoState.ProcessAuthentication(LazyAsyncResult 懒惰的结果)
在 System.Net.Security.NegotiateStream.AuthenticateAsClient(NetworkCredential 凭证、ChannelBinding 绑定、字符串 targetName、ProtectionLevel requiredProtectionLevel, TokenImpersonationLevel allowedImpersonationLevel)
在 System.Net.Security.NegotiateStream.AuthenticateAsClient(NetworkCredential 凭据、字符串目标名称、保护级别 requiredProtectionLevel, TokenImpersonationLevel allowedImpersonationLevel)
在 System.ServiceModel.Channels.WindowsStreamSecurityUpgradeProvider.WindowsStreamSecurityUpgradeInitiator.OnInitiateUpgrade(流 流、SecurityMessageProperty&remoteSecurity)

尝试搜索解决方案,但不符合我的要求,因此发布。

请帮忙...

更新 1:

@A.R.,尝试使用

client.ClientCredentials.Windows.AllowedImpersonationLevel =
    System.Security.Principal.TokenImpersonationLevel.Impersonation;

但无济于事。

更新 2:

WCF 服务配置

<system.serviceModel>
    <diagnostics performanceCounters="All" />
    <bindings>
      <netTcpBinding>
        <binding name="myBindingForLargeData" maxReceivedMessageSize="5242880" maxConnections="10">
          <readerQuotas maxDepth="64" maxStringContentLength="5242880" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
        </binding>
      </netTcpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="WCFService.ServiceBehavior"
        name="WCFService.CollectorService">
        <endpoint address="" binding="netTcpBinding" bindingConfiguration="myBindingForLargeData"
          name="netTcpEndPoint" contract="WCFService.ICollectorService" />
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          name="mexTcpEndPoint" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:8010/WCFService.CollectorService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WCFService.ServiceBehavior">
          <serviceMetadata httpGetEnabled="False"/>
          <serviceDebug includeExceptionDetailInFaults="True" />
          <serviceThrottling
          maxConcurrentCalls="32"
          maxConcurrentSessions="32"
          maxConcurrentInstances="32"
           />
        </behavior>
      </serviceBehaviors>
    </behaviors>
</system.serviceModel>

【问题讨论】:

    标签: c# winforms wcf windows-services net.tcp


    【解决方案1】:

    感谢您的所有帮助。经过几天的研究和尝试 n 错误方法后,我得到了答案 :) 好吧,我知道我迟到了,但我认为迟到总比没有好。

    这就是解决方案

    我必须对我的配置文件(客户端和服务器)进行一些更改

    在客户端我添加了&lt;security&gt;标签,如下所示

      <system.serviceModel>
        <bindings>
          <netTcpBinding>
            <binding name="netTcpEndPoint" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="5242880" maxBufferSize="5242880" maxConnections="15" maxReceivedMessageSize="5242880">
              <readerQuotas maxDepth="32" maxStringContentLength="5242880" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
              <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
             <security mode="Transport">
                <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                <message clientCredentialType="Windows" />
              </security>
            </binding>
          </netTcpBinding>
        </bindings>
        <client>
          <endpoint address="net.tcp://xx.xx.xx.xx:8010/WCFService.CollectorService/" binding="netTcpBinding" bindingConfiguration="netTcpEndPoint" contract="CloudAdapter.CloudCollectorService.ICollectorService" name="netTcpEndPoint">
          </endpoint>
        </client>
      </system.serviceModel>
    

    并且还在服务器端添加了相同的标签(WCF服务配置),如下图

    <bindings>
      <netTcpBinding>
        <binding name="myBindingForLargeData" maxReceivedMessageSize="5242880" maxConnections="10">
          <readerQuotas maxDepth="64" maxStringContentLength="5242880" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
             <security mode="Transport">
            <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
            <message clientCredentialType="Windows" />
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    

    希望这可以帮助有需要的人:)

    所以关键是让&lt;security&gt;标签在客户端和服务器配置文件上相同。

    【讨论】:

      【解决方案2】:

      基本上发生的情况是您的调用服务没有适当的凭据,就像从 WinForms 调用时那样。你需要的是一些模仿。这需要一些设置,而且有点烦人,但它会起作用。

      幸运的是,MSDN 有一个不错的小演练。
      http://msdn.microsoft.com/en-us/library/ms731090.aspx

      这里有关于该主题的更多一般信息:
      http://msdn.microsoft.com/en-us/library/ms730088.aspx

      更新:
      设置模拟标志是不够的。您必须实际模拟凭据才能使其正常工作。例如:

        // Let's assume that this code is run inside of the calling service.
        var winIdentity = ServiceSecurityContext.Current.WindowsIdentity;
        using (var impContext = winIdentity.Impersonate())
        {
          // So this would be the service call that is failing otherwise.
          return MyService.MyServiceCall();
        }
      

      【讨论】:

      • 更新问题请查收
      • 好的,你必须告诉它要冒充谁。您不能只是设置标志并期望它神奇地冒充某人。阅读第二个链接中提供的材料。
      【解决方案3】:

      查看我在这篇帖子 The server has rejected the client credentials 上的回答。

      注意安全节点。

      <bindings>
        <netTcpBinding>
          <binding name="customTcpBinding" maxReceivedMessageSize="20480000" transferMode="Streamed" >
            <security mode="None"></security>
          </binding>
        </netTcpBinding>
      </bindings>
      

      【讨论】:

      • 显式添加&lt;security mode="None"&gt;&lt;/security&gt; 也对我有用。从机器 A 运行客户端应用程序时出现“服务器已拒绝客户端凭据”错误,而不是从机器 B 运行。事实证明,这是因为机器 B 具有与我的服务器相同的 Windows 凭据(用户名和密码)机器。
      【解决方案4】:

      您在 WCF 服务上使用的身份验证模式是什么?似乎 winform 应用程序正在运行并提供正确的凭据,而您的 Windows 服务未以指定的权限运行或传递的凭据无效。当您从 winforms 与 Windwos 服务发出时,请尝试使用 Fiddler 检查您的请求并查看差异。

      【讨论】:

      • 我尝试调试我的 Windows 服务,并在尝试打开与服务客户端的连接时发现上述错误。顺便说一句,什么是 Fiddler,它对我的​​场景有帮助吗,我有一个带有 net.tcp 绑定的 WCF 服务。请告诉我
      • 我没有在 WCF 服务上隐式实现任何安全性。可能是默认的安全实现。
      • @Bravo:Fiddler 是一种工具,可以检查您通过网络的传入和传出流量。从上面的配置来看,服务使用 net.Tcp。我可以知道它是托管在 IIS 中还是自托管?您的 Windows 服务也在另一台机器上吗?
      【解决方案5】:

      对我来说,将双方(客户端+服务器)的安全模式设置为无:

      NetTcpBinding 绑定 = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None;

      (与 spinner_den_g 的答案相同,但在 C# 中 - 无需编辑 app.config)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-15
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多