WCF的配置又复杂一些了,因为WCF整合了许多其他的东西,而不仅仅是一个支持WS-Security的工具,配置文件中各项的选择性也非常的大,取值范围非常的广,这样反而让开发人员在配置的时候感觉到盲目;WCF也有自己的配置工具WCF Service Configuration Editor,可以从“工具”菜单栏或者开始菜单中加载;这里就不帖具体每项的配置了,因为配置工具中没有向导;直接帖一个可用的配置文件:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <!-- If you wan to turn on logging, uncomment the "sources" section below, and set the correct log files in sharedListeners section. --> <system.diagnostics> <sharedListeners> <!-- TODO: Please fix the log file name here! --> <add initializeData="F:TempTracesapp_messages_aliclient.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp"> <filter type=""/> </add> <!-- TODO: Please fix the log file name here! --> <add initializeData="F:TempTracesapp_tracelog_aliclient.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp"> <filter type=""/> </add> </sharedListeners> </system.diagnostics> <system.serviceModel> <!-- To turn on message logging, set these flags to "true" --> <diagnostics> <messageLogging logEntireMessage="false" logMalformedMessages="false" logMessagesAtTransportLevel="false"/> </diagnostics> <bindings> <customBinding> <binding name="Soap11CustomBinding"> <textMessageEncoding messageVersion="Soap11"/> <security defaultAlgorithmSuite="Basic128" allowSerializedSigningTokenOnReply="true" authenticationMode="MutualCertificate" requireDerivedKeys="false" securityHeaderLayout="Lax" includeTimestamp="true" keyEntropyMode="CombinedEntropy" messageProtectionOrder="SignBeforeEncrypt" messageSecurityVersion="WSSecurity10WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" requireSecurityContextCancellation="false"> <secureConversationBootstrap /> </security> <httpTransport /> </binding> </customBinding> </bindings> <behaviors> <endpointBehaviors> <behavior name="NewBehavior"> <clientCredentials> <clientCertificate findValue="CN=5" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectDistinguishedName"/> <serviceCertificate> <defaultCertificate findValue="CN=alisoft" storeLocation="CurrentUser" storeName="My" x509FindType="FindBySubjectDistinguishedName"/> <authentication customCertificateValidatorType="" certificateValidationMode="PeerOrChainTrust"/> </serviceCertificate> <issuedToken cacheIssuedTokens="false"/> </clientCredentials> </behavior> </endpointBehaviors> </behaviors> <client> <!--121.0.18.160--> <endpoint address="http://localhost:1688/webservice/AppConsumeService" behaviorConfiguration="NewBehavior" binding="customBinding" bindingConfiguration="Soap11CustomBinding" contract="ServiceReference1.AppConsumeServicePortType" name="Soap11CustomBindingPort"> <identity> <dns value="alisoft"/> <certificateReference x509FindType="FindBySubjectName"/> </identity> </endpoint> </client> </system.serviceModel> </configuration>
客户端调用代码为:
ServiceReference1.AppConsumeServicePortTypeClient c =new ServiceReference1.AppConsumeServicePortTypeClient("Soap11CustomBindingPort"); //设置保护级别为签名 c.Endpoint.Contract.ProtectionLevel = System.Net.Security.ProtectionLevel.Sign; string bal = c.checkBalance("85", "afc376c9-d14b-4820-bc77-e22878fa8ce3", 11); Console.WriteLine("bal is:"+ bal);