.net SoapHeader验证

在工作中经常用到webservice,在.net 开发中经常用到webservice,在java开发经常用到cxf.

  今天闲置没事就介绍下 .net webservice中常用到 soapheader token验证和重载。当然在正常使用中不太建议使用重载。

下面的列表概述接收和处理 SOAP 标头的基本步骤:

  1. 创建一个从 SoapHeader 派生的类,表示传入 SOAP 标头的数据。

    

  1. using System.Web.Services.Protocols;
  2.  
  3. namespace WService
  4. {
  5. /// <summary>
  6. /// Fireran
  7. /// </summary>
  8. public class MySoapHeader : SoapHeader
  9. {
  10.  
  11. public MySoapHeader()
  12. {
  13.  
  14. }
  15.  
  16. /// <summary>
  17. /// username
  18. /// </summary>
  19. public string UserName { get; set; }
  20.  
  21. /// <summary>
  22. /// ip
  23. /// </summary>
  24. public string Ip { get; set; }
  25.  
  26. /// <summary>
  27. /// token
  28. /// </summary>
  29. public string Token { get; set; }
  30. }
  31. }

  2. XML Web services 方法将 myHeader 成员指定为 MemberName 属性,接收 XML Web services 方法的 MyHeader SOAP 标头的内容。

     

  1. private MySoapHeader _mySoapHeader;
  2. public MySoapHeader mySoapHeader
  3. {
  4. get { return _mySoapHeader; }
  5. set { _mySoapHeader = value; }
  6. }

  7.         [WebMethod]
  8. [SoapHeader("mySoapHeader")]
  9. public string GetSayHello(long id)
  10. {
  11. string result = "";
  12. if (mySoapHeader==null)
  13. {
  14. result = "token is nulll";
  15. }
  16. else
  17. {
  18. if (mySoapHeader.Token.Equals("123456"))
  19. {
  20. result = "hello world" + id;
  21. }
  22. else
  23. {
  24. result = "token is err";
  25. }
  26. }
  27.  
  28. return result;
  29.  
  30. }

   3. 发布webservice  利用wsdl 工具生成代理类

  1. //------------------------------------------------------------------------------
  2. // <auto-generated>
  3. // 此代码由工具生成。
  4. // 运行时版本:2.0.50727.5466
  5. //
  6. // 对此文件的更改可能会导致不正确的行为,并且如果
  7. // 重新生成代码,这些更改将会丢失。
  8. // </auto-generated>
  9. //------------------------------------------------------------------------------
  10.  
  11. using System;
  12. using System.ComponentModel;
  13. using System.Diagnostics;
  14. using System.Web.Services;
  15. using System.Web.Services.Protocols;
  16. using System.Xml.Serialization;
  17.  
  18. //
  19. // 此源代码由 wsdl 自动生成, Version=2.0.50727.3038。
  20. //
  21.  
  22. /// <remarks/>
  23. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  24. [System.Diagnostics.DebuggerStepThroughAttribute()]
  25. [System.ComponentModel.DesignerCategoryAttribute("code")]
  26. [System.Web.Services.WebServiceBindingAttribute(Name="FireRanSoap", Namespace="http://fireran.org/")]
  27. public partial class FireRan : System.Web.Services.Protocols.SoapHttpClientProtocol {
  28.  
  29. private System.Threading.SendOrPostCallback HelloWorldOperationCompleted;
  30.  
  31. private MySoapHeader mySoapHeaderValueField;
  32.  
  33. private System.Threading.SendOrPostCallback GetSayHelloOperationCompleted;
  34.  
  35. private System.Threading.SendOrPostCallback GetSayHello1OperationCompleted;
  36.  
  37. /// <remarks/>
  38. public FireRan() {this.Url = "http://localhost:54540/FireRan.asmx";
  39. }
  40.  
  41. public MySoapHeader MySoapHeaderValue {
  42. get {
  43. return this.mySoapHeaderValueField;
  44. }
  45. set {
  46. this.mySoapHeaderValueField = value;
  47. }
  48. }
  49.  
  50. /// <remarks/>
  51. public event HelloWorldCompletedEventHandler HelloWorldCompleted;
  52.  
  53. /// <remarks/>
  54. public event GetSayHelloCompletedEventHandler GetSayHelloCompleted;
  55.  
  56. /// <remarks/>
  57. public event GetSayHello1CompletedEventHandler GetSayHello1Completed;
  58.  
  59. /// <remarks/>
  60. [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://fireran.org/HelloWorld", RequestNamespace="http://fireran.org/", ResponseNamespace="http://fireran.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
  61. public string HelloWorld() {
  62. object[] results = this.Invoke("HelloWorld", new object[0]);
  63. return ((string)(results[0]));
  64. }
  65.  
  66. /// <remarks/>
  67. public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) {
  68. return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState);
  69. }
  70.  
  71. /// <remarks/>
  72. public string EndHelloWorld(System.IAsyncResult asyncResult) {
  73. object[] results = this.EndInvoke(asyncResult);
  74. return ((string)(results[0]));
  75. }
  76.  
  77. /// <remarks/>
  78. public void HelloWorldAsync() {
  79. this.HelloWorldAsync(null);
  80. }
  81.  
  82. /// <remarks/>
  83. public void HelloWorldAsync(object userState) {
  84. if ((this.HelloWorldOperationCompleted == null)) {
  85. this.HelloWorldOperationCompleted = new System.Threading.SendOrPostCallback(this.OnHelloWorldOperationCompleted);
  86. }
  87. this.InvokeAsync("HelloWorld", new object[0], this.HelloWorldOperationCompleted, userState);
  88. }
  89.  
  90. private void OnHelloWorldOperationCompleted(object arg) {
  91. if ((this.HelloWorldCompleted != null)) {
  92. System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
  93. this.HelloWorldCompleted(this, new HelloWorldCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
  94. }
  95. }
  96.  
  97. /// <remarks/>
  98. [System.Web.Services.Protocols.SoapHeaderAttribute("MySoapHeaderValue")]
  99. [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://fireran.org/SayHello", RequestElementName="SayHello", RequestNamespace="http://fireran.org/", ResponseElementName="SayHelloResponse", ResponseNamespace="http://fireran.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
  100. [return: System.Xml.Serialization.XmlElementAttribute("SayHelloResult")]
  101. public string GetSayHello(long id) {
  102. object[] results = this.Invoke("GetSayHello", new object[] {
  103. id});
  104. return ((string)(results[0]));
  105. }
  106.  
  107. /// <remarks/>
  108. public System.IAsyncResult BeginGetSayHello(long id, System.AsyncCallback callback, object asyncState) {
  109. return this.BeginInvoke("GetSayHello", new object[] {
  110. id}, callback, asyncState);
  111. }
  112.  
  113. /// <remarks/>
  114. public string EndGetSayHello(System.IAsyncResult asyncResult) {
  115. object[] results = this.EndInvoke(asyncResult);
  116. return ((string)(results[0]));
  117. }
  118.  
  119. /// <remarks/>
  120. public void GetSayHelloAsync(long id) {
  121. this.GetSayHelloAsync(id, null);
  122. }
  123.  
  124. /// <remarks/>
  125. public void GetSayHelloAsync(long id, object userState) {
  126. if ((this.GetSayHelloOperationCompleted == null)) {
  127. this.GetSayHelloOperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetSayHelloOperationCompleted);
  128. }
  129. this.InvokeAsync("GetSayHello", new object[] {
  130. id}, this.GetSayHelloOperationCompleted, userState);
  131. }
  132.  
  133. private void OnGetSayHelloOperationCompleted(object arg) {
  134. if ((this.GetSayHelloCompleted != null)) {
  135. System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
  136. this.GetSayHelloCompleted(this, new GetSayHelloCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
  137. }
  138. }
  139.  
  140. /// <remarks/>
  141. [System.Web.Services.Protocols.SoapHeaderAttribute("MySoapHeaderValue")]
  142. [System.Web.Services.WebMethodAttribute(MessageName="GetSayHello1")]
  143. [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://fireran.org/SayHello1", RequestElementName="SayHello1", RequestNamespace="http://fireran.org/", ResponseElementName="SayHello1Response", ResponseNamespace="http://fireran.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
  144. [return: System.Xml.Serialization.XmlElementAttribute("SayHello1Result")]
  145. public string GetSayHello(int id, int name) {
  146. object[] results = this.Invoke("GetSayHello1", new object[] {
  147. id,
  148. name});
  149. return ((string)(results[0]));
  150. }
  151.  
  152. /// <remarks/>
  153. public System.IAsyncResult BeginGetSayHello1(int id, int name, System.AsyncCallback callback, object asyncState) {
  154. return this.BeginInvoke("GetSayHello1", new object[] {
  155. id,
  156. name}, callback, asyncState);
  157. }
  158.  
  159. /// <remarks/>
  160. public string EndGetSayHello1(System.IAsyncResult asyncResult) {
  161. object[] results = this.EndInvoke(asyncResult);
  162. return ((string)(results[0]));
  163. }
  164.  
  165. /// <remarks/>
  166. public void GetSayHello1Async(int id, int name) {
  167. this.GetSayHello1Async(id, name, null);
  168. }
  169.  
  170. /// <remarks/>
  171. public void GetSayHello1Async(int id, int name, object userState) {
  172. if ((this.GetSayHello1OperationCompleted == null)) {
  173. this.GetSayHello1OperationCompleted = new System.Threading.SendOrPostCallback(this.OnGetSayHello1OperationCompleted);
  174. }
  175. this.InvokeAsync("GetSayHello1", new object[] {
  176. id,
  177. name}, this.GetSayHello1OperationCompleted, userState);
  178. }
  179.  
  180. private void OnGetSayHello1OperationCompleted(object arg) {
  181. if ((this.GetSayHello1Completed != null)) {
  182. System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
  183. this.GetSayHello1Completed(this, new GetSayHello1CompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
  184. }
  185. }
  186.  
  187. /// <remarks/>
  188. public new void CancelAsync(object userState) {
  189. base.CancelAsync(userState);
  190. }
  191. }
  192.  
  193. /// <remarks/>
  194. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  195. [System.SerializableAttribute()]
  196. [System.Diagnostics.DebuggerStepThroughAttribute()]
  197. [System.ComponentModel.DesignerCategoryAttribute("code")]
  198. [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://fireran.org/")]
  199. [System.Xml.Serialization.XmlRootAttribute(Namespace="http://fireran.org/", IsNullable=false)]
  200. public partial class MySoapHeader : System.Web.Services.Protocols.SoapHeader {
  201.  
  202. private string userNameField;
  203.  
  204. private string passWordField;
  205.  
  206. private string tokenField;
  207.  
  208. private System.Xml.XmlAttribute[] anyAttrField;
  209.  
  210. /// <remarks/>
  211. public string UserName {
  212. get {
  213. return this.userNameField;
  214. }
  215. set {
  216. this.userNameField = value;
  217. }
  218. }
  219.  
  220. /// <remarks/>
  221. public string PassWord {
  222. get {
  223. return this.passWordField;
  224. }
  225. set {
  226. this.passWordField = value;
  227. }
  228. }
  229.  
  230. /// <remarks/>
  231. public string Token {
  232. get {
  233. return this.tokenField;
  234. }
  235. set {
  236. this.tokenField = value;
  237. }
  238. }
  239.  
  240. /// <remarks/>
  241. [System.Xml.Serialization.XmlAnyAttributeAttribute()]
  242. public System.Xml.XmlAttribute[] AnyAttr {
  243. get {
  244. return this.anyAttrField;
  245. }
  246. set {
  247. this.anyAttrField = value;
  248. }
  249. }
  250. }
  251.  
  252. /// <remarks/>
  253. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  254. public delegate void HelloWorldCompletedEventHandler(object sender, HelloWorldCompletedEventArgs e);
  255.  
  256. /// <remarks/>
  257. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  258. [System.Diagnostics.DebuggerStepThroughAttribute()]
  259. [System.ComponentModel.DesignerCategoryAttribute("code")]
  260. public partial class HelloWorldCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  261.  
  262. private object[] results;
  263.  
  264. internal HelloWorldCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  265. base(exception, cancelled, userState) {
  266. this.results = results;
  267. }
  268.  
  269. /// <remarks/>
  270. public string Result {
  271. get {
  272. this.RaiseExceptionIfNecessary();
  273. return ((string)(this.results[0]));
  274. }
  275. }
  276. }
  277.  
  278. /// <remarks/>
  279. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  280. public delegate void GetSayHelloCompletedEventHandler(object sender, GetSayHelloCompletedEventArgs e);
  281.  
  282. /// <remarks/>
  283. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  284. [System.Diagnostics.DebuggerStepThroughAttribute()]
  285. [System.ComponentModel.DesignerCategoryAttribute("code")]
  286. public partial class GetSayHelloCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  287.  
  288. private object[] results;
  289.  
  290. internal GetSayHelloCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  291. base(exception, cancelled, userState) {
  292. this.results = results;
  293. }
  294.  
  295. /// <remarks/>
  296. public string Result {
  297. get {
  298. this.RaiseExceptionIfNecessary();
  299. return ((string)(this.results[0]));
  300. }
  301. }
  302. }
  303.  
  304. /// <remarks/>
  305. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  306. public delegate void GetSayHello1CompletedEventHandler(object sender, GetSayHello1CompletedEventArgs e);
  307.  
  308. /// <remarks/>
  309. [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "2.0.50727.3038")]
  310. [System.Diagnostics.DebuggerStepThroughAttribute()]
  311. [System.ComponentModel.DesignerCategoryAttribute("code")]
  312. public partial class GetSayHello1CompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
  313.  
  314. private object[] results;
  315.  
  316. internal GetSayHello1CompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
  317. base(exception, cancelled, userState) {
  318. this.results = results;
  319. }
  320.  
  321. /// <remarks/>
  322. public string Result {
  323. get {
  324. this.RaiseExceptionIfNecessary();
  325. return ((string)(this.results[0]));
  326. }
  327. }
  328. }

  4. 在代理类中添加 token 信息  修改代理类中的构造方法 如下

public FireRan() {
  MySoapHeaderValue = new MySoapHeader();
  this.MySoapHeaderValue.Token = "123456";
  this.Url = "http://localhost:54540/FireRan.asmx";
}

    

  5. 添加测试类进行测试

  1. [TestFixture]
  2. public class FireRanTest
  3. {
  4. [Test]
  5. public void Read()
  6. {
  7. using(FireRan fireRan = new FireRan())
  8. {
  9. Console.Write(fireRan.GetSayHello(1));
  10. }
  11.  
  12. }
  13. }

    6. 运行结果

  

  在项目中为了服务安全对token和ip进行授权是很有必要的。但是在项目中有发现token 和 ip 很难精确的控制服务的访问。建议在实际开发中可以根据自己业务添加动态的验证方式,和每个业务独立的业务类型。这样可以更加精确的控制访问着的访问权限和权限降级。

  

遇见了就不要错过
 

.net SoapHeader验证的更多相关文章

  1. C#访问Java的WebService添加SOAPHeader验证的问题

    原文:C#访问Java的WebService添加SOAPHeader验证的问题 这两天做与公司OA的接口,发现C#访问Java的WebService需要提供一个SOAP的头验证信息,但是WebServ ...

  2. C#调用Java的WebService添加SOAPHeader验证(2)

    C#调用Java的WebService添加SOAPHeader验证 上一篇链接如上,更像是 Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) 来处理xml, ...

  3. C#调用Java的WebService添加SOAPHeader验证

    C#调用Java的WebService添加SOAPHeader验证(2) 1.问题描述 调用的Java的webservice string Invoke(string func, string req ...

  4. .net 客户端调用java或.net webservice进行soapheader验证

    .net 客户端调用java或.net webservice进行soapheader验证 最近项目中有业务需要跨平台调用web服务,客户端和服务器之间采用非对称加密来保证数据的安全性,webservi ...

  5. C#动态调用带有SoapHeader验证的WebServices

    http://blog.csdn.net/u012995964/article/details/54573143 本文记录C#中通过反射动态的调用带有SoapHeader验证的WebServices服 ...

  6. C#静态调用带有SoapHeader验证的WebServices

    转自:http://blog.csdn.net/u012995964/article/details/54562111 本文记录带有SoapHeader验证的WebServices服务创建.部署及C# ...

  7. ANDROID调用webservice带soapheader验证

    最近的一个项目中调用webservice接口,需要验证soapheader,现将解决方法记录如下:(网上资料出处太多,就不做引用,原作者如看到,如有必要添加请通知) 1.先看接口 POST /webs ...

  8. Webservice加上SoapHeader验证方式

    提供一种基于SoapHeader的自定义验证方式,代码如下: public class MySoapHeader : System.Web.Services.Protocols.SoapHeader ...

  9. webservice 第一节 .net SoapHeader验证

    在工作中经常用到webservice,在.net 开发中经常用到webservice,在java开发经常用到cxf. 今天闲置没事就介绍下 .net webservice中常用到 soapheader ...

随机推荐

  1. Msgbox消息框

    于"自"<软件开发工具>一本书有这样的制剂,让我回忆--程序员可以做,让用户做:它允许用户做的少,即使是那些谁需要做的程序设置.我们应该做的. 这不是宣言.该是践行. ...

  2. 深入理解C指针之六:指针和结构体

    原文:深入理解C指针之六:指针和结构体 C的结构体可以用来表示数据结构的元素,比如链表的节点,指针是把这些元素连接到一起的纽带. 结构体增强了数组等集合的实用性,每个结构体可以包含多个字段.如果不用结 ...

  3. Objective-c中的单例

    单例是指静态分配的实例,就是只开辟一块内存,不会重新开辟内存,而 iphone sdk 中全是这种实例,例如[UIApplication sharedApplication] 返回一个指向代表应用程序 ...

  4. 如何使用Visual Studio 2013 开发PHP5.6项目

    原文如何使用Visual Studio开发PHP项目 在windows下开发php除了记事本 DW 以及一帮Zend studio,Eclipse,NetBeans之流以外,个人感觉还是vsiual ...

  5. 在windows下用C语言写socket通讯实例

    原文:在windows下用C语言写socket通讯实例 From:Microsoft Dev Center #undef UNICODE #define WIN32_LEAN_AND_MEAN #in ...

  6. php soap调用asp.net webservice

    原文:php soap调用asp.net webservice 首先做一下准备工作,找到安装环境里的php.ini把;extension=php_soap.dll去掉前面的;.我这里使用的是wamp, ...

  7. QT中出现“无法解析的外部符号”错误

    串口中我要把奇偶校验等enum数据对应写到一个ComboBox中去,所以需要在我的Settings类中加入一个函数用来转换. 这里,我首先在settings.h的class Settings中加上对该 ...

  8. ser2net使用

    在ubuntu下或者openwrt下安装了ser2net程序之后,可以将串口中的数据转发为以太网数据. 设置在/etc/ser2net.conf中最后: 3002:0:/dv/ttyUSB0:1152 ...

  9. JQUERY简写案例

    源代码: <script ttype="text/javascript"> $(function(){ $(".btn").eq(0).click( ...

  10. html5 音频和视频(audio And video)

    1.音频和视频  Web 上的视频 直到现在,仍然不存在一项旨在网页上显示视频的标准. 今天,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. HTML5 规定 ...