服务器已拒绝客户端凭据,WCF作为Windows服务

Bra*_*avo 9 c# wcf windows-services net.tcp winforms

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

服务器已拒绝客户端凭据

内部异常:System.Security.Authentication.InvalidCredentialException:服务器已拒绝客户端凭据.
---> System.ComponentModel.Win32Exception:登录尝试失败
---内部异常堆栈跟踪结束---
在System.Net.Security.NegotiateStream.AuthenticateAsClient处的System.Net.Security.NegoState.ProcessAuthentication(LazyAsyncResult lazyResult)
处(的NetworkCredential凭证,ChannelBinding结合,字符串目标名称,的ProtectionLevel requiredProtectionLevel,TokenImpersonationLevel allowedImpersonationLevel)
在System.Net.Security.NegotiateStream.AuthenticateAsClient(的NetworkCredential凭证,字符串目标名称,的ProtectionLevel requiredProtectionLevel,TokenImpersonationLevel allowedImpersonationLevel)
在System.ServiceModel.Channels.WindowsStreamSecurityUpgradeProvider.WindowsStreamSecurityUpgradeInitiator. OnInitiateUpgrade(Stream stream,SecurityMessageProperty&remoteSecurity)

尝试寻找解决方案,但没有符合我的要求,因此发布.

请帮忙...

更新1:

@AR,尝试使用

client.ClientCredentials.Windows.AllowedImpersonationLevel =
    System.Security.Principal.TokenImpersonationLevel.Impersonation;
Run Code Online (Sandbox Code Playgroud)

但无济于事.

更新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>
Run Code Online (Sandbox Code Playgroud)

Bra*_*avo 6

感谢你的帮助.经过几天的研究和试验n错误方法后我得到了答案:)我知道我发布答案的时间已经很晚了,但我认为它迟到总比没有好.

所以这是解决方案

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

在客户端,我添加了<security>标签,如下所示

  <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>
Run Code Online (Sandbox Code Playgroud)

并在服务器端添加相同的标记(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>
Run Code Online (Sandbox Code Playgroud)

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

因此,KEY是使<security>标记在客户端和服务器配置文件上相同.