在應用別人接口的時候,總是要用簽名,很是不理解簽名這是怎么知道做的。通過對Attribute的學習了解。大體可以用Attribute來做簽名應用。
具體過程如下:
首先我們要先定義一個類,該類繼承Attribute。該類主要最用是,簽名需要用到的方法、參數和獲取加密文件
1 public class CashiSongAttribute : Attribute 2 { 3 /// <summary> 4 /// 簽名參數 5 /// </summary> 6 public string[] Param { get; set; } 7 /// <summary> 8 /// 是否簽名 9 /// </summary>10 public bool IsSign { get; set; }11 /// <summary>12 /// 加密文件13 /// </summary>14 /// <param name="bp"></param>15 /// <param name="mi"></param>16 /// <returns></returns>17 public string ParamEncryption(BasePage bp,System.Reflection.MethodInfo mi)18 {19 if (Param != null && Param.Length > 0)20 {21 string md5 = "op" + mi.Name.ToLower();22 foreach (string item in Param)23 {24 if (item.ToLower() == "op" || item.ToLower() == "sign")25 continue;26 md5 += item + bp.GetRequest(item);27 }28 byte[] bytestr = Encoding.Default.GetBytes(md5);29 MD5 _md5 = new MD5CryptoServicePRovider();30 byte[] bytesend = _md5.ComputeHash(bytestr);31 return BitConverter.ToString(bytesend).Replace("-", "");32 }33 return "";34 }35 }View Code
新建一個頁面,在該頁面創建一個方法,并加入該特性
1 [CashiSong(IsSign = true, Param = new string[] { "op", "name" })]2 public string getceshicon()3 {4 return "簽名成功!";5 }
下面關鍵就再也通過調用方式的時候,驗證參數是否都符合,加密文件是否正確。
創建一個基類BasePage,該基類主要負責,接受參數,并指定參數指定的方法,并判斷簽名信息是否正確。
這里會用到:System.Reflection.MethodInfo的應用、獲取特性Attribute參數內容。
public class BasePage : Page { public BasePage() { this.Load += new EventHandler(BasePage_Load); } void BasePage_Load(object sender, EventArgs e) { Response.AddHeader("Accept-Charset","UTF-8"); string op = GetRequest("op"); if (!string.IsNullOrEmpty(op)) { System.Reflection.MethodInfo mi = this.GetType().GetMethod(op); Attribute_Jude(mi); } this.Response.End(); } /// <summary> /// 簽名判斷 /// </summary> /// <param name="mi"></param> public void Attribute_Jude(MethodInfo mi) { MsgModel Msg = new MsgModel(); if (mi.IsDefined(typeof(CashiSongAttribute), false)) { object[] attrs = mi.GetCustomAttributes(typeof(CashiSongAttribute), false); CashiSongAttribute iplimit = (CashiSongAttribute)attrs[0]; object responsestr=null; if (iplimit != null && iplimit.Param.Length > 0) { string server_sign = GetRequest("sign"); string client_sign = iplimit.ParamEncryption(this, mi); if (!server_sign.Equals(client_sign, StringComparison.OrdinalIgnoreCase)&&iplimit.IsSign) { Msg.msg = "Sing Error"; Msg.toile = 0; Send(Msg); return; } responsestr = mi.Invoke(this, null); } Msg.toile = 1; Msg.msg = responsestr.ToString(); Send(Msg); } } public void Send(MsgModel Msg) { Response.AddHeader("Content-type","applictaion/json"); javaScriptSerializer Javascript = new JavaScriptSerializer(); string Con = javaScript.Serialize(Msg); Response.Write(Con); } public string GetRequest(string key) { if (Request.QueryString[key] == null) return ""; else return Request.QueryString[key]; } } public class MsgModel { public string msg { get; set; } public int toile { get; set; } }
獲取特性參數內容的方法(CashiSongAttribute 為自定義特性類)
if (mi.IsDefined(typeof(CashiSongAttribute), false)) { object[] attrs = mi.GetCustomAttributes(typeof(CashiSongAttribute), false); CashiSongAttribute iplimit = (CashiSongAttribute)attrs[0]; }
新聞熱點
疑難解答