2012-08-14 142 views
4

我有一個WCF服務,它使用NetTcpBinding與消息安全和用戶名認證。在此之前,我使用WsHttpBinding,但我切換到NetTcp,因爲我可以使用回調。WCF NetTcpBinding安全

我服務的配置是這樣的:

<service behaviorConfiguration="WcfServiceLibrary1.ServiceBehavior" name="WcfServiceLibrary1.Service"> 

    <endpoint 
      address="net.tcp://localhost:9000/Design_Time_Addresses/WcfServiceLibrary1/Service/" 
      binding="netTcpBinding" 
      bindingConfiguration="NetTCPbinding" 
      contract="WcfServiceLibrary1.IService" 
      name="NetTCPBinding"> 
    </endpoint> 
    <host> 
     <baseAddresses> 
     <add baseAddress="http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service/" /> 
     </baseAddresses> 
    </host> 
    </service> 
</services> 
<bindings> 
    <netTcpBinding> 
    <binding name="NetTCPbinding"> 
     <security mode="Message"> 
     <message clientCredentialType="UserName"/> 
     </security> 
    </binding> 
    </netTcpBinding> 
</bindings> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="WcfServiceLibrary1.ServiceBehavior"> 
     <serviceCredentials> 
     <serviceCertificate findValue="ServerCert" 
          storeLocation="CurrentUser" 
          storeName="TrustedPeople" 
          x509FindType="FindBySubjectName" /> 
     <userNameAuthentication 
      userNamePasswordValidationMode="MembershipProvider" 
      membershipProviderName="CustomMembershipProvider" /> 
     </serviceCredentials> 
     <!-- To avoid disclosing metadata information, 
     set the value below to false and remove the metadata endpoint above before deployment --> 
     <serviceMetadata httpGetEnabled="True" /> 
     <!-- To receive exception details in faults for debugging purposes, 
     set the value below to true. Set to false before deployment 
     to avoid disclosing exception information --> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
     <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="MyRoleProvider" /> 
     <!-- Logs when an authentication failure --> 
     <serviceSecurityAudit auditLogLocation="Application" 
          suppressAuditFailure="true" 
          serviceAuthorizationAuditLevel="Failure" 
          messageAuthenticationAuditLevel="SuccessOrFailure" /> 
    </behavior> 
    </serviceBehaviors> 
</behaviors> 

我認爲-in這種情況下,證明書的使用,因此該服務可以登錄所有它發送給客戶端的數據服務的憑證,所以客戶端可以知道它正在與正確的服務進行通信。

客戶端的配置如下:

<behaviors> 
    <endpointBehaviors> 
    <behavior name="messageSecurityBehavior"> 
     <clientCredentials> 
     <clientCertificate storeLocation="CurrentUser" 
          storeName="TrustedPeople" 
          x509FindType="FindBySubjectName" 
          findValue="ClientCert" /> 
     <serviceCertificate> 
      <authentication certificateValidationMode="None" /> 
     </serviceCertificate> 
     </clientCredentials> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 
<bindings> 
    <netTcpBinding> 
    <binding name="NetTCPBinding" closeTimeout="00:01:00" openTimeout="00:01:00" 
       receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" 
       transferMode="Buffered" transactionProtocol="OleTransactions" 
       hostNameComparisonMode="StrongWildcard" listenBacklog="10" 
       maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" 
       maxReceivedMessageSize="65536"> 
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
      maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
     <reliableSession ordered="true" inactivityTimeout="00:10:00" 
      enabled="false" /> 
     <security mode="Message"> 
     <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" /> 
     <message clientCredentialType="UserName" /> 
     </security> 
    </binding> 
    </netTcpBinding> 

</bindings> 
<client> 
    <endpoint address="net.tcp://localhost:9000/Design_Time_Addresses/WcfServiceLibrary1/Service/" 
      binding="netTcpBinding" bindingConfiguration="NetTCPBinding" 
      contract="IService" name="NetTCPBinding" behaviorConfiguration="messageSecurityBehavior"> 
    <identity> 
     <certificate encodedValue="AwAAAAEAAAAUAAAAQerRMlEg2a66HMxD/8El6LutassgAAAAAQAAAOwBAAAwggHoMIIBVaADAgECAhAzOWVhNWUzM2Y5MzUwNzFhMAkGBSsOAwIdBQAwIjELMAkGA1UEBhMCVVMxEzARBgNVBAMTClNlcnZlckNlcnQwHhcNMTIwMTE3MTk1NDQ2WhcNMjUwOTI1MTk1NDQ2WjAiMQswCQYDVQQGEwJVUzETMBEGA1UEAxMKU2VydmVyQ2VydDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAs6SaYi3pDDIOuXeCe7HgOOTvOpwajKdxM8MzZpkK3l+DqkriMQUx9DT5I9WZvNK5FMuorLueG5adWkTgWpvttC7Sp8CJ//A+PbPewiAU4L3Txln6dX3jHZFd99LY/58/2AzY8ln2NykQFzz1DdmVeyShG9ktVsX82Ogz60lzpeUCAwEAAaMnMCUwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDgYDVR0PBAcDBQD6AAAAMAkGBSsOAwIdBQADgYEAMGQ7fIW34CYjybCc0gWOLaxeRFiNHmX/dgxqPIWGgg3uc0avz1GHJ3uMHcPAZBKcw9QpbccALpqZxZzkDBxufWjRni+8XCzeAKUqaB7XaJTYYUYBbz7/2+EuaAw7/vF4JTtZGWhPkHZZcX5+Oyo2ktK0z24MfXP2Ggy+IsQJ2JM=" /> 
    </identity> 
    </endpoint> 
</client> 

所有這一切工作正常。我讀過服務證書用於加密客戶端證書和消息。那麼爲什麼我們需要AlgorithmSuite中指定的算法?它加密了什麼?

我需要理解這個,所以我可以在我的論文中解釋它。

回答

1

證書導致密鑰用於加密郵件(我不確定它是否直接用作密鑰本身,或用於協商密鑰)。 AlgorithmSuite確定algorithm,它將使用該密鑰來加密消息。


爲什麼你需要指定一個算法?
因爲您需要告訴WCF 如何它應該加密您的消息。所使用的算法也在SOAP消息中標識,因爲接收方必須知道使用什麼算法來解密消息。請注意,如果您沒有明確設置算法,它仍然會有一個值,即NetTcp的默認Basic256(source)。

如果啓用消息跟蹤,你會看到沿SOAP體這些線路的東西適當的(加密)的行動爲您服務操作:

<e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes256-cbc" /> 

如果你想改變AlgorithmSuite到Basic128的以上將在算法屬性的後一位中顯示aes128-cbc

在消息跟蹤中,就在與您的服務操作對應的操作之前,http://schemas.xmlsoap.org/ws/2005/02/trust/...範圍內會有幾個操作來協商所有安全選項。在Message Security in WCF MSDN頁面上,您可以看到WCF使用WS-Security規範。有關此規格的更多信息,您可以前往this page,我找到WS-Security 2004 spec (pdf)。如果您需要官方的論文參考資料,這也可能有用。

什麼它加密?
該算法對證書和消息(使用在證書幫助下獲得的密鑰)進行加密,就像您已經說過的那樣:-)。如果您啓用message tracing,則可以看到這一點:憑證和郵件正文將使用選定的算法進行加密。


免責聲明:我仍然在某處WCF學習曲線的斜率,因此,如果你的論文等級取決於這個你最好仔細檢查我的主張:d