2014-01-24 39 views
3

我有以下代碼:對SSPI的調用失敗,請在第二次運行調用時查看內部​​異常?

public GetUserDataResponse GetUserDataFromService(X509Certificate2 certificate) 
{ 
    ChannelFactory<MyApp4SITHSService.IMyApp4SITHSServiceContract> factory = new ChannelFactory<MyApp4SITHSService.IMyApp4SITHSServiceContract>("NetTcpBinding_IMyApp4SITHSServiceContract_Certificate"); 
    MyApp4SITHSService.IMyApp4SITHSServiceContract service; 
    GetUserDataResponse response; 

    factory.Credentials.ClientCertificate.Certificate = certificate; 
    //factory.Credentials.UserName.UserName = "me"; 
    //factory.Credentials.UserName.Password = "password"; 

    service = factory.CreateChannel(); 

    LogHandler.WriteLine("Connecting to service"); 
    response = service.GetUserData(new GetUserDataRequest()); 
    LogHandler.WriteLine("Data received"); 

    factory.Abort(); 
    return response; 
} 

我第一次這樣運行它workes好了,我第二次獲得以下異常的service.GetUserData:

第一次機會發生類型'System.ServiceModel.Security.SecurityNegotiationException'的異常mscorlib.dll中的

對SSPI的調用失敗,請參閱內部異常。

本地安全機構無法使用以下配置聯絡

林:

<system.serviceModel> 
    <behaviors> 
    <endpointBehaviors> 
     <behavior name="CertificateEndpointBehavior"> 
     <clientCredentials> 
      <!--<clientCertificate findValue="MyAppClient" x509FindType="FindBySubjectName" storeLocation="CurrentUser" storeName="TrustedPeople"/>--> 
      <!--<clientCertificate findValue="MyAppClient" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="My"/>--> 
      <serviceCertificate> 
      <authentication certificateValidationMode="ChainTrust" revocationMode="NoCheck"/> 
      </serviceCertificate> 
     </clientCredentials> 
     </behavior> 
    </endpointBehaviors> 
    </behaviors> 
    <bindings> 
     <netTcpBinding> 
      <binding name="netTcpCertificate" closeTimeout="00:01:00" openTimeout="00:01:00" 
       receiveTimeout="Infinite" sendTimeout="01:00:00" transactionFlow="false" 
       transferMode="Buffered" transactionProtocol="OleTransactions" 
       hostNameComparisonMode="StrongWildcard" listenBacklog="1000" 
       maxBufferPoolSize="2147483647" maxBufferSize="2147483647" 
       maxConnections="200" maxReceivedMessageSize="2147483647"> 
       <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" 
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> 
       <reliableSession ordered="true" inactivityTimeout="Infinite" 
        enabled="false" /> 
       <security mode="Transport"> 
        <transport clientCredentialType="Certificate" /> 
        <message clientCredentialType="Certificate" /> 
       </security> 
      </binding> 
     </netTcpBinding> 
    </bindings> 
    <client> 
     <endpoint address="net.tcp://localhost:8135/MyApp4SITHSService/Client/sll" 
      behaviorConfiguration="CertificateEndpointBehavior" binding="netTcpBinding" 
      bindingConfiguration="netTcpCertificate" contract="MyApp4SITHSService.IMyApp4SITHSServiceContract" 
      name="NetTcpBinding_IMyApp4SITHSServiceContract_Certificate"> 
      <identity> 
       <dns value="MyAppServer" /> 
      </identity> 
     </endpoint> 
    </client> 
</system.serviceModel> 

任何想法,爲什麼我得到這個問題,以及如何解決呢?

回答

2

原因是該證書可能不被驗證爲可信,這裏是一些鏈接,試圖查看是否有任何關閉。也張貼您的整個錯誤堆棧。 link one link two

代碼示例

<endpointBehaviors> 

    <behavior name="ClientCertificateBehavior"> 

     <clientCredentials> 

     <clientCertificate findValue="www.testclient.eu" 

       storeLocation="CurrentUser" storeName="My"        x509FindType="FindBySubjectName" /> 

     <serviceCertificate> 

      <authentication 

       trustedStoreLocation="CurrentUser" 

       certificateValidationMode="PeerTrust"/> 



      <defaultCertificate 

      findValue='www.testsvc.eu' 

      storeLocation='CurrentUser' 

      storeName='My' 

      x509FindType='FindBySubjectName' /> 

     </serviceCertificate> 



     </clientCredentials> 

    </behavior> 

    </endpointBehaviors> 

</behaviors> 
+0

但這怎麼可能?它第一次正常工作?第二次是使用與第一次相同的代碼完成的? – Banshee

3

服務器沒有權限聯繫本地計算機證書存儲驗證的信任的證書通過

+1

是的,這聽起來是正確的,但爲什麼它在第一次嘗試,但從未在以下嘗試?它可以驗證成功,爲什麼這不是第二次工作? – Banshee

1

請查看「身份」 web.config或App.config文件中的元素。如果存在「身份」元素,則評論或刪除它。你的問題將得到解決。

相關問題