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>
/// Summary of MySoapHeader
/// </Summary>
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>
/// Summary of WebService
/// </Summary>
[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 "));
}
}