微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WebService安全处理办法之一

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

 

  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. }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐