在應用別人接口的時候,老是要用簽名,非常不理解簽名這是怎麼知道作的。經過對Attribute的學習瞭解。大致能夠用Attribute來作簽名應用。java
具體過程以下:json
首先咱們要先定義一個類,該類繼承Attribute。該類主要最用是,簽名須要用到的方法、參數和獲取加密文件app
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 }
新建一個頁面,在該頁面建立一個方法,並加入該特性ide
1 [CashiSong(IsSign = true, Param = new string[] { "op", "name" })] 2 public string getceshicon() 3 { 4 return "簽名成功!"; 5 }
下面關鍵就再也經過調用方式的時候,驗證參數是否都符合,加密文件是否正確。學習
建立一個基類BasePage,該基類主要負責,接受參數,並指定參數指定的方法,並判斷簽名信息是否正確。this
這裏會用到: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 爲自定義特性類)spa
if (mi.IsDefined(typeof(CashiSongAttribute), false)) { object[] attrs = mi.GetCustomAttributes(typeof(CashiSongAttribute), false); CashiSongAttribute iplimit = (CashiSongAttribute)attrs[0]; }