2012-01-09 120 views
8

我能够使用Win-form应用程序连接到我的WCF服务,但是我无法使用我的Windows服务进行此操作。每当我火open()来代理它抛出以下错误服务器已拒绝客户端凭据,WCF作为Windows服务

服务器拒绝了客户端证书

内部异常:System.Security.Authentication.InvalidCredentialException:服务器 拒绝了客户端证书。
---> System.ComponentModel.Win32Exception:登录尝试失败
---内部异常堆栈跟踪的结尾---
在System.Net.Security.NegoState.ProcessAuthentication(LazyAsyncResult lazyResult)
在System.Net.Security.NegotiateStream.AuthenticateAsClient(的NetworkCredential 凭证,ChannelBinding结合,字符串目标名称,的ProtectionLevel requiredProtectionLevel,TokenImpersonationLevel allowedImpersonationLevel)
在System.Net.Security.NegotiateStream.AuthenticateAsClient(的NetworkCredential 凭证,字符串目标名称,保护ionLevel 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> 

回答

4

感谢您的帮助。我在几天的一些研究和试验中得到了答案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> 

并且还在服务器端添加了相同的标签(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> 

希望这有助于有需要的人:)

所以关键是让<security>标签同样在客户端和服务器的配置文件。

4

基本上正在发生的事情是,你的呼叫服务没有适当的凭据,如从的WinForms致电时,你就会有。你需要的是一些模仿。它需要一些设置,有点烦人,但它会起作用。

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

有在这里的话题更多一些常规信息:
http://msdn.microsoft.com/en-us/library/ms730088.aspx

UPDATE:
设置模拟标志是不够的。您必须实际模仿凭证才能使其工作。例如:

// 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(); 
    } 
+0

更新了问题请检查 – Bravo 2012-01-09 14:54:08

+0

好的,你必须真实地告诉它是谁模仿。你不能只设置旗帜,并期望它神奇地冒充某人。阅读第二个链接中提供的材料。 – 2012-01-09 15:27:00

1

什么是您在WCF服务上使用的身份验证模式?看起来像winform应用程序正在运行,并提供正确的凭据,而您的Windows服务未运行指定的权限或传递的凭据无效。尝试使用Fiddler检查您的请求,当您使用Winforms和Windwos服务制作并查看其差异时。

+0

我试图调试我的Windows服务,当我尝试打开服务客户端的连接时发现上述错误。顺便说一句什么是提琴手,并在我的情况下,我有一个WCF服务与net.tcp绑定有帮助。请让我知道 – Bravo 2012-01-09 15:00:56

+0

我没有隐式地在WCF服务上实现任何安全性。可能是默认的安全实现。 – Bravo 2012-01-09 15:06:50

+0

@Bravo:Fiddler是一个工具,可以检查网络上的传入和传出流量。从上面的配置服务使用net.Tcp。我可以知道它是否托管在IIS中或自己托管?另外你的Windows服务在不同的机器上? – Rajesh 2012-01-09 15:21:15

1

查看我对此文章的回复The server has rejected the client credentials

请注意安全节点。

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

这对我有效 – 2013-03-19 09:47:03

相关问题