In websevrice, the SOAP header is very important, mainly for security considerations. In Asp.net 2.0, you can simply apply the SOAP Header
For silly applications, more complex applications must be further viewed,
First, we will write a simple helloworld WebService.
Using system;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
Public class service: system. Web. Services. WebService
{
Public validationsoapheader authentication;
Private const string dev_token = "12345 ";
Public Service ()
{
// Uncomment the following line if using designed components
// Initializecomponent ();
}
[Soapheader ("authentication")]
[Webmethod]
Public String helloworld ()
{
If (authentication! = NULL & authentication. devtoken = dev_token)
{
Return "Hello World ";
}
Else
{
Throw new exception ("authentication failed ");
}
}
}
As you can see, you must first add [soapheader ("authentication")] before [webmethod]. Here, we determine whether the SOAP header from the client is valid, check whether the tokern is equal to the token we have defined in advance. If yes, output Hello world; otherwise, an exception is thrown.
Here we write a validationsoapheader class, which inherits the SOAP header, as shown below:
Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Web. Services. Protocols;
/// <Summary>
/// Summary description for ephonecredentials
/// </Summary>
Public class validationsoapheader: soapheader
{
Private string _ devtoken;
Public validationsoapheader ()
{
}
Public validationsoapheader (string devtoken)
{
This. _ devtoken = devtoken;
}
Public String devtoken
{
Get {return this. _ devtoken ;}
Set {This. _ devtoken = value ;}
}
}
Here, we only read some attribute settings, and then write a client, as shown below, explicitly specifying the SOAP header to be passed
// Lelemycsharpclient is the project name of the client.
Localhost. validationsoapheader header = new consolemycsharpclient. localhost. validationsoapheader ();
Header. devtoken = "12345 ";
Localhost. Service Ws = new consolemycsharpclient. localhost. Service ();
WS. validationsoapheadervalue = header;
Console. writeline (WS. helloworld ());
Console. Readline ();
In this way, after the client passes 12345 to WS, the Hello world will be displayed correctly; otherwise, the Hello world will not be displayed correctly.