Web Service authentication solution 1: authentication by using the SOAP Header.
1. We implement a class for identity authentication, with the file name MySoapHeader. cs
The MySoapHeader class inherits from System. Web. Services. Protocols. SoapHeader. Two member variables, UserName and PassWord, are defined, and a user-authenticated function ValideUser is defined. It provides the UserName and PassWord check function.
- Using System;
- Using System. Data;
- Using System. Configuration;
- Using System. Web;
- Using System. Web. Security;
- Using System. Web. UI;
- Using System. Web. UI. HtmlControls;
- Using System. Web. UI. WebControls;
- Using System. Web. UI. WebControls. WebParts;
- Using System. Web. Services;
- Using System. Web. Services. Protocols;
- ///
- /// Summary of MySoapHeader
- ///
- Public class MySoapHeader: SoapHeader
- {
- Public MySoapHeader ()
- {
- //
- // TODO: add the constructor logic here
- //
- }
- Public string UserName;
- Public string PassWord;
- Public bool ValideUser (string in_UserName, string in_PassWord)
- {
- If (in_UserName = "zxq") & (in_PassWord = "123456 "))
- {
- Return true;
- }
- Else
- {
- Return false;
- }
- }
- }
2. The following Code creates WebService. asmx WebService. cs:
- Using System;
- Using System. Collections;
- Using System. Web;
- Using System. Web. Services;
- Using System. Web. Services. Protocols;
-
- ///
- /// Summary of WebService
- ///
- [WebService (Namespace = "http://tempuri.org/")]
- [WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
- Public class WebService: System. Web. Services. WebService
- {
-
- Public WebService ()
- {
-
- // If you use the designed component, uncomment the following line
- // InitializeComponent ();
- }
- Public MySoapHeader header; // defines the user authentication class variable header
- [WebMethod (Description = "user verification test")]
- [System. Web. Services. Protocols. SoapHeader ("header")] // soap header for User Authentication
- Public string HelloWorld (string contents)
- {
- // Verify the access permission
- If (header. ValideUser (header. UserName, header. PassWord ))
- {
- Return contents + "executed ";
- }
- Else
- {
- Return "You are not authorized to access ";
- }
- }
- }
3. Create a Default. aspx Client
- Default. aspx. cs code
-
- Using System;
- Using System. Configuration;
- Using System. Data;
- Using System. Web;
- Using System. Web. Security;
- Using System. Web. UI;
- Using System. Web. UI. HtmlControls;
- Using System. Web. UI. WebControls;
- Using System. Web. UI. WebControls. WebParts;
-
- Public partial class _ Default: System. Web. UI. Page
- {
- Protected void Page_Load (object sender, EventArgs e)
- {
- Com. cn1yw. WebService test = new com. cn1yw. WebService (); // web reference (changed to your own)
- Com. cn1yw. MySoapHeader Header = new com. cn1yw. MySoapHeader (); // create a soap Header object by referencing the web (change it to your own)
- // Set the soap header variable
- Header. UserName = "zxq ";
- Header. PassWord = "123456 ";
- Test. MySoapHeaderValue = Header;
- // Call the web method
- Response. Write (test. HelloWorld ("I am strong "));
- }
- }
Web Service authentication solution 2: windows authentication is integrated.
1. Set the web service program to integrated windows Authentication
2. Client web reference code
- Test. WebReference. Service1 wr = new Test. WebReference. Service1 (); // generate a web service instance
- Wr. Credentials = new NetworkCredential ("guest", "123"); // guest is the user name, which requires certain permissions.
- LblTest. Text = wr. Add (2, 2). ToString (); // call the web service Method
The advantage of this solution is that it is relatively safe and has good performance. The disadvantage is that it is not easy to transplant and the deployment workload is large.