2012-01-09 122 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

相關問題