Study Notes-based WCF security (3) Digital Certificate + custom verification (wshttpbinding)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.