Use SOAP header in Asp.net 2.0

Source: Internet
Author: User

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.

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.