Authentication Policy in Web Service-use custom SOAP Headers

Source: Internet
Author: User
You can customize the SOAP header to restrict the service calling user range from 1 using System;
2 using System. Web;
3 using System. Web. Services;
4 using System. Web. Services. Protocols;
5
6 [WebService (Namespace = "http://livebaby.cn")]
7 [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
8 public class Service: System. Web. Services. WebService
9 {
10 public SecurityHeader currentUser;
11 public Service ()
12 {
13
14 // if you use the designed component, uncomment the following line
15 // InitializeComponent ();
16}
17 [WebMethod, SoapHeader ("currentUser")]
18 public string GetResult (string queryString)
19 {
20 if (ValidateUser (currentUser. UserName, currentUser. UserPass ))
21 {
22 return "the string you sent is:" + queryString;
23}
24 else
25 return "Sorry:" + currentUser. UserName + ", you are not a legal user! ";
26}
27 // check the SOAP HEADER
28 private bool ValidateUser (string user, string pass)
29 {
30 if (user. Equals ("user") & pass. Equals ("user "))
31 return true;
32 else
33 return false;
34}
35}
36 // custom Soap Header Class
37 public class SecurityHeader: System. Web. Services. Protocols. SoapHeader
38 {
39 public string UserName;
40 public string UserPass;
41}

The following is the client's call of 1 using System;
2 using System. Collections. Generic;
3 using System. ComponentModel;
4 using System. Data;
5 using System. Drawing;
6 using System. Text;
7 using System. Windows. Forms;
8
9 namespace SoapHeader
10 {
11 public partial class Form1: Form
12 {
13 public Form1 ()
14 {
15 InitializeComponent ();
16}
17
18 private void button_Invoke_Click (object sender, EventArgs e)
19 {
20 SoapHeader. localhost. SecurityHeader header = new SoapHeader. localhost. SecurityHeader ();
21 header. UserName = textBox_User.Text;
22 header. UserPass = textBox_Pass.Text;
23 SoapHeader. localhost. Service service = new SoapHeader. localhost. Service ();
24 service. SecurityHeaderValue = header;
25 this. textBox_Output.Text + = service. GetResult (this. textBox_Input.Text) + Environment. NewLine;
26}
27}
28}
29

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.