Let's take a look at the third topic of WCF to verify the identity of a user, and the third topic of wcf.

Source: Internet
Author: User

Let's take a look at the third topic of WCF to verify the identity of a user, and the third topic of wcf.

Today, I mainly write a message about the use of header messages to verify user information.

I will draw a picture below to see the picture first

Step 1: Start the user request code first.

First, create an executable context object block and define the internal transmission channel.

Using (OperationContextScope scope = new OperationContextScope (userClient. InnerChannel ))

Then: Create the SOAP transmission content for the header message to be sent

MessageHeader myHeaderUid = MessageHeader. CreateHeader (
"Header", "uid", UId );

Header: The local name of the header XML Element (the name can be obtained arbitrarily, but must be consistent with that on the server side)

Uid: The namespace URI of the header XML Element (the name can be obtained arbitrarily, but must be consistent with that on the server side)

UId: The information to be transmitted (an object-type value)

Finally, add the header content to the object to be transmitted.

OperationContext. Current. OutgoingMessageHeaders. Add (myHeaderUid );

OK to complete the client encoding.

1 ValidUserClient userClient = new ValidUserClient (); 2 while (true) 3 {4 Console. write ("username:"); 5 string UId = Console. readLine (); 6 Console. write ("Password:"); 7 string Pwd = Console. readLine (); 8 using (OperationContextScope scope = new OperationContextScope (userClient. innerChannel) // create an executable context object block and define the internal Transmission Channel 9 {10 MessageHeader myHeaderUid = MessageHeader. createHeader (11 "header", "uid", UId); 12 MessageHeader myHeaderPwd = MessageHeader. createHeader (13 "header", "pwd", Pwd); 14 OperationContext. current. outgoingMessageHeaders. add (myHeaderUid); 15 OperationContext. current. outgoingMessageHeaders. add (myHeaderPwd); 16 Console. writeLine (userClient. user (); 17} 18}Client code

Step 2: server code

First, start retrieving the header message.

Int uIdIndex = OperationContext. current. incomingMessageHeaders. findHeader ("header", "uid"); (be sure to keep consistent with the client, what we get is the index, that is, the header information location (if index =-1, there is no header message ))

Then, retrieve the header message content Uid = OperationContext through the index. current. incomingMessageHeaders. getHeader <string> (uIdIndex); finally, we perform logical judgment 1 Dictionary <string, string> AllUsers = new Dictionary <string, string> (10 ); 2 public bool User () 3 {4 InsertUser (); // dynamically Add User 5 string Uid = string in a simulated database. empty; 6 string Pwd = string. empty; 7 bool result = false; 8 int uIdIndex = OperationContext. current. incomingMessageHeaders. findHeader ("head Er "," uid "); 9 int pwdIndex = OperationContext. Current. IncomingMessageHeaders. FindHeader (" header "," pwd "); 10 if (uIdIndex! =-1) 11 {12 Uid = OperationContext. Current. IncomingMessageHeaders. GetHeader <string> (uIdIndex); 13} 14 if (pwdIndex! =-1) 15 {16 Pwd = OperationContext. current. incomingMessageHeaders. getHeader <string> (pwdIndex); 17} 18 if (AllUsers. containsKey (Uid) 19 {20 if (AllUsers [Uid] = Pwd) 21 {22 result = true; 23} 24 25} 26 return result; 27} 28 29 private void InsertUser () 30 {31 AllUsers. add ("admin"," 123456 "); 32 AllUsers. add ("sa", "123456"); 33 AllUsers. add ("ldc", "123456"); 34 AllUsers. add ("zd", "123456"); 35}Server code

In this way, we can simply verify the user with the header message. I have written a lot of bad things that you can come up with and learn together. The next article describes handwritten message protocols and data protocols.

Source code download

Related Article

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.