1.NET中撰写需要使用SoapHeader验证的Web Service
代码1:WebService.cs
using System.Web.Services; using System.Web.Services.Protocols;
[WebService(Namespace = "http://www.microsoft.com/")] //名字空间,注意FLEX中调用的时候要手工填写 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class WebService : System.Web.Services.WebService {
public WebService () {}
public AuthenticationInfo authenticationInfo;
[WebMethod] [SoapHeader("authenticationInfo")] //为服务添加SoapHeader public string HelloWorld() { if (authenticationInfo == null) { return "验证信息不能为空." } else{ if (Authentication.Check(authenticationInfo.username, authenticationInfo.password)) { return "Hello world!" } else{ return "用户名密码验证失败,你没有权力访问此服务。" } } } }
//用户密码验证类 public class Authentication { public static bool Check(string username, string password) { return ((username == "user") && (password == "password")); } }
代码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> /// 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中调用使用了SoapHeader的Web Service
代码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 button1_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 在Flex调用此Web Service
代码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(); //先清理一下 var auinfo:Authentication = new Authentication(txtUser.text,txtPassword.text); // ws.addHeader(auinfo); //添加 ws.HelloWorld(); //调用服务 } 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>
代码5:AuthenticationInfo.as
package{ public class AuthenticationInfo{ //字段名称需和.NET端一致 public var username:String; public var password:String; } }
代码6:Authentication.as
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); } } }