In Websevrice, the SOAP header is very important, well, primarily for security reasons, in ASP. NET 2.0, you can simply apply the SOAP header to
For a fool-like application, more complex applications of course to go deeper to see,
First, let's 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], where we determine if the SOAP header from the client is valid, and see if its tokern is equal to our pre-defined token, and if yes, output Hello World, or throw an exception
Here we write a Validationsoapheader class, which inherits the SOAP header, as follows
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 is just a set of properties to read, and then write a client, as below, explicitly specifying the SOAP header to pass
Consolemycsharpclient is the project name that establishes 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 ();
This way, when the client passes 12345 to WS, the Hello World is displayed correctly, otherwise it will not display correctly
Using SOAP headers for dummies in ASP. NET 2.0