加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

WebService安全处理办法之一

发布时间:2020-12-17 02:40:00 所属栏目:安全 来源:网络整理
导读:?平常我经常使用动态随机数加密验证的办法,可以验证客户端的合法性,不过这种方法也蛮不错的。 ? using ?System; using ?System.Collections; using ?System.ComponentModel; using ?System.Data; using ?System.Linq; using ?System.Web; using ?System.We

?平常我经常使用动态随机数加密验证的办法,可以验证客户端的合法性,不过这种方法也蛮不错的。

?

  1. using?System;
  2. using?System.Collections;
  3. using?System.ComponentModel;
  4. using?System.Data;
  5. using?System.Linq;
  6. using?System.Web;
  7. using?System.Web.Services;
  8. using?System.Web.Services.Protocols;
  9. using?System.Xml.Linq;
  10. namespace?TallyInfo.TIWebService
  11. {
  12. ????///?<summary>
  13. ????///?ClientAuthenticate?的摘要说明
  14. ????///?</summary>
  15. ????[WebService(Namespace?=?"http://tempuri.org/")]
  16. ????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
  17. ????[ToolboxItem(false)]
  18. ????//?若要允许使用?ASP.NET?AJAX?从脚本中调用此?Web?服务,请取消对下行的注释。
  19. ????//?[System.Web.Script.Services.ScriptService]
  20. ????public?class?ClientAuthenticate?:?System.Web.Services.WebService
  21. ????{
  22. ????????#region?客户端身份认证
  23. ????????#region?GetTicket
  24. ????????[WebMethod]?//(Description?=?"票据产生方法,客户端在调用其它方法之前必须先调用该方法认证其身份,验证成功的结果就是返回一个票据")
  25. ????????public?string?GetTicket(string?identity,?string?password)
  26. ????????{
  27. ????????????//Authenticate?the?client
  28. ????????????if?(!Authenticate(identity,?password))
  29. ????????????{
  30. ????????????????throw?new?Exception("Invalid?identity/Password");
  31. ????????????}
  32. ????????????Guid?gTicket?=?Guid.NewGuid();
  33. ????????????this.Context.Cache.Insert(gTicket.ToString(),?true);
  34. ????????????return?gTicket.ToString();
  35. ????????}
  36. ????????#endregion?GetTicket
  37. ????????#region?Authenticate
  38. ????????///?<summary>
  39. ????????///?验证票据
  40. ????????///?</summary>
  41. ????????///?<param?name="ticket"></param>
  42. ????????///?<returns></returns>
  43. ????????private?bool?Authenticate(string?ticket)
  44. ????????{
  45. ????????????try
  46. ????????????{
  47. ????????????????bool?bRet?=?false;
  48. ????????????????if?((bool)Context.Cache.Get(ticket))
  49. ????????????????{
  50. ????????????????????bRet?=?true;
  51. ????????????????}
  52. ????????????????return?bRet;
  53. ????????????}
  54. ????????????catch?(NullReferenceException?NullEx)
  55. ????????????{
  56. ????????????????throw?NullEx;
  57. ????????????}
  58. ????????????catch?(Exception?Ex)
  59. ????????????{
  60. ????????????????throw?Ex;
  61. ????????????}
  62. ????????}
  63. ????????#endregion?Authenticate
  64. ????????#region?Authenticate
  65. ????????///?<summary>
  66. ????????///?获取票据之前到数据库验证客户身份
  67. ????????///?</summary>
  68. ????????///?<param?name="identity"></param>
  69. ????????///?<param?name="password"></param>
  70. ????????///?<returns></returns>
  71. ????????private?bool?Authenticate(string?identity,?string?password)
  72. ????????{
  73. ????????????bool?retAuth?=?false;
  74. ????????????string?SQLString?=?"SELECT?*?FROM?DataProviders?WHERE?标识码?=?'"?+?identity?+?"'?AND?口令?=?'"?+?password?+?"'";
  75. ????????????//DataSet?ds?=?DbHelperSQL.Search(SQLString);
  76. ????????????//if?(ds.Tables[0].Rows.Count?==?1)
  77. ????????????retAuth?=?true;
  78. ????????????return?retAuth;
  79. ????????}
  80. ????????
  81. ????????#endregion?Authenticate
  82. ????????#endregion?客户端身份认证
  83. ????????#region?SearchWithSQL???执行例程
  84. ????????[WebMethod]
  85. ????????public?DataSet?SearchWithSQL(string?SQLString,?string?ticket)
  86. ????????{
  87. ????????????//在执行方法体响应之前验证票据有效性
  88. ????????????if?(!Authenticate(ticket))
  89. ????????????{
  90. ????????????????throw?new?Exception("Invalid?Ticket");
  91. ????????????}
  92. ????????????return?null;
  93. ????????????//return?DbHelperSQL.Search(SQLString);
  94. ????????}
  95. ????????#endregion?SearchWithSQL????执行例程
  96. ????}
  97. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读