Using SOAP headers for dummies in ASP. NET 2.0

Source: Internet
Author: User

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

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.