Original address: http://www.cnblogs.com/jfzhu/p/4071342.html
Reprint please indicate the source
The previous article describes how the message security mode of WCF BasicHttpBinding BasicHttpBinding And clientCredentialType is used by certificate.
This article demonstrates that BasicHttpBinding uses transport Security Mode and Clientcredentialtype= "None".
(i) WCF Service code and configuration file
IDemoService.cs
using System.ServiceModel; namespace Wcfdemo { "idemoservice")] Public Interface Idemoservice { [OperationContract] [Faultcontract (typeof(Dividebyzerofault))] int Divide (intint denominator);} }
DemoService.cs
usingSystem;usingSystem.ServiceModel;usingSystem.ServiceModel.Activation;namespaceWcfdemo {[Aspnetcompatibilityrequirements (Requirementsmode=aspnetcompatibilityrequirementsmode.allowed)] Public classDemoservice:idemoservice { Public intDivide (intNumerator,intdenominator) { Try { returnNumerator/denominator; } Catch(DivideByZeroException ex) {Dividebyzerofault fault=NewDividebyzerofault (); Fault. Error=Ex. Message; Fault. Detail="denominator cannot be zero!"; Throw NewFaultexception<dividebyzerofault>(fault); } } } }
The complete code can also be found in the WCF service creation and throw strongly typed soap Fault.
Server Web. config
<?XML version= "1.0"?> <Configuration> <system.web> <compilationDebug= "true"targetframework= "4.0" /> </system.web> <System.ServiceModel> <Bindings> <BasicHttpBinding> <bindingname= "Basicbinding"> <SecurityMode= "Transport"> <TransportclientCredentialType= "None" /> </Security> </binding> </BasicHttpBinding> </Bindings> <Services> <Servicename= "Wcfdemo.demoservice"behaviorconfiguration= "Custombehavior"> <EndpointAddress= "Demoservice"binding= "BasicHttpBinding"Contract= "Wcfdemo.idemoservice"bindingconfiguration= "Basicbinding" /> <EndpointAddress= "Mex"binding= "mexHttpBinding"Contract= "IMetadataExchange"></Endpoint> </Service> </Services> <Behaviors> <servicebehaviors> <Behaviorname= "Custombehavior"> <Servicemetadatahttpsgetenabled= "true" /> <ServicedebugIncludeexceptiondetailinfaults= "false" /> </Behavior> </servicebehaviors> </Behaviors> <servicehostingenvironmentmultiplesitebindingsenabled= "true" /> </System.ServiceModel> </Configuration>
(ii) Add an HTTPS binding for the WCF Service application.
See the step by step configuration for ASP. NET Web application using HTTPS.
After configuring the HTTPS binding, double-click SSL Settings
Tick require SSL and click Apply.
The binding of HTTP is still indispensable, otherwise the following error will occur
(iii) Installing the SSL root certificate on the client
Because the HTTPS certificate is using the
So we're using a WCF Service URL of https://win-ounm08eqe64.henry.huang/DemoService.svc
On the client, add a record for C:\Windows\System32\Drivers\etc\host
Then install the root certificate
Double-click the root certificate file to eject the dialog box for the certificate properties, at which point the root certificate is not trusted, we need to add it to "trusted Root Certification Authorities", click Install Certificate
(iv) Client code and configuration files
Add service Reference to client Visual Studio
Private void Buttoncalculate_click (object sender, EventArgs e) { try { = demoserviceclient.divide (Convert.ToInt32 (Textboxnumerator.text), Convert.ToInt32 ( Textboxdenominator.text)). ToString (); } Catch (faultexception<demoservicereference.dividebyzerofault> fault) { "" + fault. Detail.detail); } }
Client App. Config
<?XML version= "1.0" encoding= "Utf-8"?> <Configuration> <System.ServiceModel> <Bindings> <BasicHttpBinding> <bindingname= "Basichttpbinding_idemoservice"> <SecurityMode= "Transport" /> </binding> </BasicHttpBinding> </Bindings> <Client> <EndpointAddress= "Https://win-ounm08eqe64.henry.huang/DemoService.svc/DemoService"binding= "BasicHttpBinding"bindingconfiguration= "Basichttpbinding_idemoservice"Contract= "Demoservicereference.idemoservice"name= "Basichttpbinding_idemoservice" /> </Client> </System.ServiceModel> </Configuration>
(v) running code, listening to message
Use Fiddler to find all messages encrypted
However, if you view the message log with the Microsoft Service Trace Viewer (see Using the Trace and message Log feature of WCF), you can see the decrypted information because it is not listening on a wire, And fiddler is listening on The wire.
Request:
Response:
(vi) Summary
Transport security mode is an encryption at the transport protocol level, and message Security mode is an encryption of the messaging level. Each protocol has its own transport protocol level of encryption, such as HTTP encryption is SSL.
WCF basichttpbinding Transport Security Mode, clientcredentialtype= "None"