On the basis of the previous two sections!
Add system. identitymodel;/system. identitymodel. selectors to the host;
Add a new verification class:
Using system; using system. collections. generic; using system. LINQ; using system. text;/* Add the following namespaces */using system. identitymodel; using system. identitymodel. tokens; using system. identitymodel. selectors; namespace wcfhost {class validate: usernamepasswordvalidator // inherits the username and password setting {public override void validate (string username, string password) {If (username! = PASSWORD) {console. writeline ("Login Failed! : {0} ", username); throw new securitytokenexception (" unknown username or password ");} else {console. writeline (" Login sucessfully! : {0} ", username );}}}}
Add a class that specifies the User-Defined Authentication Mode Based on the configuration file in the previous section. The configuration file is as follows:
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <system. web> <compilation DEBUG = "true"/> </system. web> <system. servicemodel> <services> <service name = "WCF. service1 "behaviorconfiguration =" WCF. service1behavior ">
Update the client service reference in the previous chapter. In fact, the configuration file does not change or change anything else. Basically, the client has not changed the user name and password for verification purposes.
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net; using system. net. security; using system. security. cryptography. x509certificates; namespace wcfclient {class program {static void main (string [] ARGs) {mywcf. service1client client = new wcfclient. mywcf. service1client ();/* Add the following two for verification, which is the same as the code in the previous section, but is not verified by Windows users */client. clientcredentials. username. username = "ASEN"; client. clientcredentials. username. password = "ASEN"; system. net. servicepointmanager. servercertificatevalidationcallback + = mycertificatevalidate; console. write (client. getdata ("the value of the expired client! "); Console. Read ();} Private Static bool mycertificatevalidate (Object sender, x509certificate cert, x509chain chain, sslpolicyerrors error) {// trust any certificate !!! System. Console. writeline ("Warning, trust any certificate"); Return true ;}}}
So far, a custom verification is complete!
The key here is to create a custom authentication class and add the class to the configuration file.