在应用别人接口的时候,总是要用签名,很是不理解签名这是怎么知道做的。通过对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]; }
新闻热点
疑难解答