学习笔记之 WCF (自定义SoapHeader,做安全)
2009-11-13 20:41
302 查看
1,自定义SoapHeader类 ,必须继承SoapHeader类,下面引用老徐的一段代码:
2,通过自定义类对webService进行处理:
[SoapHeader("_authenticationToken")] 设轩webService 的SoapHeader 属性
通过_authenticationToken != null && UserValidation.IsUserLegal(_authenticationToken.Token )中的_authenticationToken来得到客户端的值
全部代码:
3,客户端,用VS2008引用webservice ref 把webService引用进来(下实例代码中把这个proxy命名为localhost)
至此做完了!
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; using System.Web.Services.Protocols;//SoapHeader处于此命名空间 /// <summary> /// Coded By Frank Xu Lei 2/17/2009 http://www.cnblogs.com/frank_xl/ /// MySoapHeader 定义用来传递来自用户的 Soap消息的Soap Header信息 /// </summary> public class MySoapHeader : SoapHeader//用户自定义SoapHeader类必须继承于SoapHeader { //属性,存储用户票据 private string _token; //构造函数 public MySoapHeader() { // // TODO: Add constructor logic here // } //重载构造函数 public MySoapHeader(string sToken) { this._token = sToken; } //属性访问器 public string Token { get { return this._token; } set { this._token = value; } } }
2,通过自定义类对webService进行处理:
[SoapHeader("_authenticationToken")] 设轩webService 的SoapHeader 属性
通过_authenticationToken != null && UserValidation.IsUserLegal(_authenticationToken.Token )中的_authenticationToken来得到客户端的值
全部代码:
using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; using WebServiceUserValidation;//这里只是难证类,下面的IsUserLegal只是做了两个字符串的是否相等 [WebService(Namespace = "http://www.cnblogs.com/frank_xl/")]//定义WebService的命名空间,为避免重复,通常设置为URL地址 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class FrankXuWebService : System.Web.Services.WebService { public MySoapHeader _authenticationToken;//存储包含用户票据Soap Header信息的属性字段,MySoapHeader继承自SoapHeader,我们在服务端定义实现 public FrankXuWebService() { } //定义SoapHeader传递的方向, //SoapHeaderDirection.In;只发送SoapHeader到服务端 //SoapHeaderDirection.Out;只发送SoapHeader到客户端 //SoapHeaderDirection.InOut;发送SoapHeader到服务端和客户端 //SoapHeaderDirection.Fault;服务端方法异常的话,会发送异常信息到客户端 [SoapHeader("_authenticationToken")] //设置方法SoapHeader属性,用于WebService的方法处理Soap Header消息 //"_authenticationToken"就是定义的FrankXuWebService类的私有属性名称, [WebMethod(EnableSession=false)] public string HelloFrank() { //要想在浏览器直接使用Invoke测试服务,这个用户票据代码要注销,不然会无法调用 if (_authenticationToken != null && UserValidation.IsUserLegal(_authenticationToken.Token ))//验证票据的值是否正确, { return "Hello Frank,WebMethod is called sucessfully..."; } else { throw new Exception("Authentication Failed"); } } }
3,客户端,用VS2008引用webservice ref 把webService引用进来(下实例代码中把这个proxy命名为localhost)
using System; using System.Collections.Generic; using System.Text; namespace ConsoleWebServiceClient { class Program { static void Main(string[] args) { localhost.MySoapHeader mySoapHeader = new ConsoleWebServiceClient.localhost.MySoapHeader();//实例化一个简单对象访问协议的头,SoapHeader mySoapHeader.Token = "FrankXuLei";//对象设置客户端知道的票据的值 string sResult = string.Empty; localhost.FrankXuWebService frankXuWebService = null; try { frankXuWebService = new ConsoleWebServiceClient.localhost.FrankXuWebService();//实例化一个客户端引用Web服务的类 frankXuWebService.MySoapHeaderValue = mySoapHeader;//**********************设置Web服务的SoapHeader sResult = frankXuWebService.HelloFrank();//调用Web服务的HelloWorld()方法。 Console.WriteLine(sResult); } catch (Exception ex) { // Console.WriteLine("Call WebService is failed..."); throw ex; } finally { //释放托管资源 if (frankXuWebService != null) frankXuWebService.Dispose(); } //调试 Console.WriteLine("Press any key to continue..."); Console.ReadLine(); } } }
至此做完了!
相关文章推荐
- 学习笔记之 WCF安全(6) 数字证书 自定义证书验证+ 客户端 证书验证(wshttpBinding)
- 学习笔记之 WCF安全(3) 数字证书+ 自定义验证(wshttpBinding)
- IssueVision 学习笔记(一)-----使用SoapHeader传递Web Serivices自定义的身份验证数据
- 学习笔记之 WCF安全(1) SSL安全,无客户端验证(wsHttpBinding)
- WCF学习笔记(六)自定义绑定和C/S之间传输流的绑定
- java安全框架-Shiro学习笔记(七)-自定义realm
- 学习笔记之 WCF安全(2) 基本身份验证(windows账号密码)(wsHttpBinding)
- 删除自定义服务 寒江孤钓<<windows 内核安全编程>> 学习笔记
- 学习笔记之 WCF安全(4) 集成window身份验证 (netTCP)
- WCF学习笔记之传输安全
- WCF学习笔记三:使用自定义行为扩展WCF总结
- 学习笔记之 WCF安全(5) 数字证书+ 客户端证书验证(wshttpBinding)
- WCF学习笔记之传输安全
- 工作学习笔记——C#自定义类型剪贴板操作、自定义鼠标光标定位问题
- 【安全牛学习笔记】tcpdump简介及常用命令实例
- Silverlight学习笔记[4] - 在Silverlight中通过WCF连接ORACLE DB数据库(译)
- Django学习笔记 — 自定义User模型
- WCF身份验证二:基于消息安全模式的自定义身份验证
- Spring Boot学习笔记-错误处理及自定义
- 信息安全学习笔记-简单linux命令与c语言编程(一)