Customize authentication and authorization using SOAP Headers in Web Services

Source: Internet
Author: User
Use the SOAP header to customize authentication and authorization

Windows authentication is very suitable for the Intranet solution. In this case, you verify the identity of users in your own domain. However, on the internet, you may need to perform custom authentication and authorization on the SQL database. In this case, you should pass custom creden。 (such as usernames and passwords) to the service and allow the Service to handle authentication and authorization.

An easy way to pass additional information together with the request to the XML Web Service is through the SOAP header. Therefore, you need to defineSoapheaderThen declare the public fields of the service as this type. This is made public in the public contract of the Service and can be used by the client when a proxy is created from webserviceutil.exe, as shown in the following example:

Using System. Web. Services;
Using System. Web. Services. Protocols;

// Authheader class extends from soapheader
Public   Class Authheader: soapheader {
Public StringUsername;
Public StringPassword;
}

Public   Class Headerservice: WebService {
PublicAuthheader sheader;

}

EachWebmethodAll can be usedSoapheaderCustom Attributes define a set of associated headers. By default, headers are required, but optional headers can also be defined.SoapheaderAttribute specifies the name of a public field orClientOrServerClass attributes (calledHeadersAttribute ). Before calling a method for the input header,WebServiceSetHeadersAttribute value. When the method returns an output header,WebServiceSearch for this value. For more information about the output or optional headers, see. NET Framework SDK documentation.

[Webmethod (Description = " This method requires a custom SOAP header set by the caller " )]
[Soapheader ( " Sheader " )]
Public   String Securemethod () {

If (Sheader =   Null )
Return   " Error: Please supply Credentials " ;
Else
Return   " User: "   + Sheader. Username;

}

Then, the client directly sets the header on the proxy class before calling the method that requires the header, as shown in the following example:

 

Headerservice H =   New Headerservice ();
Authheader myheader =   New Authheader ();
Myheader. Username =   " Johndoe " ;
Myheader. Password =   " Password " ;
H. authheader = Myheader;
String result = H. securemethod ();

 

See http://chs.gotdotnet.com/quickstart/aspplus/doc/secureservices.aspx

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.