Web service with soapheader added to the flex call

Source: Internet
Author: User

1. Write a web service that requires soapheader authentication in. net

Code 1: WebService. CS

Using system. Web. Services;
Using system. Web. Services. Protocols;

[WebService (namespace = "http://www.microsoft.com/")] // namespace, pay attention to the flex call to manually fill in
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
Public class WebService: system. Web. Services. WebService {

Public WebService (){}

Public authenticationinfo;

[Webmethod]
[Soapheader ("authenticationinfo")] // Add soapheader to the service
Public String helloworld (){
If (authenticationinfo = NULL ){
Return "the verification information cannot be blank ."
}
Else {
If (authentication. Check (authenticationinfo. username, authenticationinfo. Password )){
Return "Hello world! "
}
Else {
Return "failed to verify the user name and password. You have no right to access this service. "
}
}
}
}

// User password verification class
Public class authentication {
Public static bool check (string username, string password ){
Return (username = "user") & (Password = "password "));
}
}

Code 2: authenticationinfo. CS

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;

/// <Summary>
/// Abstract description of authenticationinfo
/// </Summary>
Public class authenticationinfo: system. Web. Services. Protocols. soapheader {
Public authenticationinfo (){}
Public authenticationinfo (string username, string password ){
This. Username = username;
This. Password = password;
}
Public String username;
Public String password;
}

2. Net calls the Web service using the soapheader

Code 3: form1.cs

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsapplication1 {
Public partial class form1: FORM {
Public form1 (){
Initializecomponent ();
}
Private void button#click (Object sender, eventargs e ){
Localhost. WebService Service = new windowsapplication1.localhost. WebService ();
Localhost. authenticationinfo Au = new windowsapplication1.localhost. authenticationinfo ();
Au. Username = textbox1.text;
Au. Password = textbox2.text;
Service. authenticationinfovalue = AU;
Label3.text = service. helloworld ();
}
}
}

3. Call this web service in flex

Code 4: soapheadertest. mxml

<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX = "http://www.adobe.com/2006/mxml" layout = "absolute" fontfamily = "simsun" fontsize = "12">
<Mx: SCRIPT>
<! [CDATA [
Import MX. rpc. Events. faultevent;
Import MX. rpc. Events. resultevent;
Import MX. rpc. Soap. soapheader;
Private function invoke (): void {
WS. clearheaders (); // clear it first
VaR auinfo: authentication = new authentication (txtuser. Text, txtpassword. Text );//
WS. addheader (auinfo); // Add
WS. helloworld (); // call the service
}
Private function wsresult (Event: resultevent): void {
Lblinformation. Text = event. Result. tostring ();
}
Private function wsfault (Event: faultevent): void {
Lblinformation. Text = event. Message. tostring ();
}
]>
</MX: SCRIPT>
<Mx: WebService id = "ws" WSDL = "http: // localhost: 4443/websites/WebService. asmx? WSDL"
Result = "wsresult (event)" fault = "wsfault (event)"/>
<Mx: textinput id = "txtuser" x = "98" Y = "94"/>
<Mx: textinput id = "txtpassword" x = "98" Y = "124"/>
<Mx: Label x = "29" Y = "98" text = "User:"/>
<Mx: Label x = "29" Y = "128" text = "Password:"/>
<Mx: button x = "193" Y = "166" label = "INVOKE" Click = "INVOKE ()"/>
<Mx: Label x = "29" Y = "213" text = "label" id = "lblinformation"/>
</MX: Application>

Code 5: authenticationinfo.

Package {
Public class authenticationinfo {// The field name must be consistent with that of. net.
Public var Username: string;
Public var password: string;
}
}

Code 6: authentication.

Package {
Import MX. rpc. Soap. soapheader;

Public class authentication extends soapheader {
Public var Username: string;
Public var password: string;
Public Function authentication (User: String, password: string ){
VaR content: authenticationinfo = new authenticationinfo ();
Content. Username = user;
Content. Password = password;
VaR QNAME: QNAME = new QNAME ("http://www.microsoft.com/", "authenticationinfo ");
Super (QNAME, content );
}
}
}

Related Article

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.