Case download
http://download.csdn.net/detail/woxpp/4113172
The client calls the code through the proxy class
Proxy generation See
Http://www.cnblogs.com/woxpp/p/6232298.html
X509 Certificate Creation
Http://www.cnblogs.com/woxpp/p/6232325.html
Custom user name password verification requires certificate support
Server-side Configuration code
<system.serviceModel> <services> <service name="Wcfservicelibrary1.service1"behaviorconfiguration="Custombehavior"> "http://localhost:8732/WcfServiceLibrary"/> </baseAddresses> "net.tcp://localhost:8731/wcfservicelibrary"binding="nettcpbinding"bindingconfiguration="testnettcpbinding"contract="Wcfservicelibrary1.iservice1"> <!--at deployment time, the following identity elements should be removed or replaced to reflect the identity used to run the deployed service. After deletion, WCF automatically infers the identity. -<identity> <dns value="localhost"/> </identity> </endpoint> <!--Metadata Endpoints--<!--metadata Exchange endpoint For the appropriate service to introduce itself to the client. -<!--This endpoint does not use a secure binding, you should ensure it is secure or removed before deployment-<endpoint address="Mex"binding="mexhttpbinding"contract="IMetadataExchange"/> </service> </services> <bindings> <netTcpBinding> <binding name="testnettcpbinding"> <security mode="Message"> <message clientcredentialtype="UserName"/> </security> </binding> </netTcpBinding> </bindings> <behaviors > <serviceBehaviors> <behavior name="Custombehavior"> <!--to avoid leaking metadata information, set the following values before deployment tofalseand delete the above metadata end point--<servicemetadata httpgetenabled="True"/> <!--to receive fault exception details for debugging, set the following values totrue。 Before deployment, set tofalseto avoid leaking exception information-<servicedebug includeexceptiondetailinfaults="False"/> <serviceCredentials> <servicecertificate findvalue="TestServer"Storename="My"storelocation="CurrentUser"X509findtype="Findbysubjectname"/> <usernameauthentication usernamepasswordvalidationmode="Custom"Customusernamepasswordvalidatortype="Wcfservicelibrary1.mycustomvalidator,wcfservicelibrary1"/> </serviceCredentials> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
Server-side custom validation code
namespacewcfservicelibrary1{ Public classMycustomvalidator:usernamepasswordvalidator { Public Override voidValidate (stringUserName,stringpassword) { //Validate Arguments if(string. IsNullOrEmpty (userName))Throw NewArgumentNullException ("UserName"); if(string. IsNullOrEmpty (password))Throw NewArgumentNullException ("Password"); //Check if the user is not Xiaozhuang if(UserName! ="abcd1234"|| Password! ="abcd1234") Throw NewSecuritytokenexception ("user name or password is wrong! "); } }}
Private voidbtnTest_Click (Objectsender, EventArgs e) { //service1client client = new Service1client (); //txtmessage.text = client. Getdatausingdatacontract (New Wcfservicelibrary1.compositetype () {stringvalue = "sssss"}). StringValue;nettcpbinding binding2=Newnettcpbinding (); Binding2. Security.mode=Securitymode.message; Binding2. Security.message=NewMessagesecurityovertcp () {clientCredentialType =Messagecredentialtype.username}; EndpointAddress Endpoint=NewEndpointAddress (NewUri ("net.tcp://localhost:8731/wcfservicelibrary"), Endpointidentity.creatednsidentity ("TestServer")); ChannelFactory<IService1> factory =NewChannelfactory<iservice1>(binding2, endpoint); Factory. Credentials.ClientCertificate.SetCertificate (Storelocation.currentuser, storename.my, X509FINDTYPE.FINDBYSUBJ Ectname,"TestServer"); Factory. Credentials.UserName.UserName="abcd1234"; Factory. Credentials.UserName.Password="abcd1234"; IService1 Client=Factory. CreateChannel (); Txtmessage.text= client. Getdatausingdatacontract (NewWcfservicelibrary1.compositetype () {stringvalue ="sssss" }). StringValue; //B9DF5B912B8CF8EAB07A7BB9B0D17694522AB0CE}
Custom user name password verification for WCF security