C# Attribute應用:類簽名

來源:互聯網
上載者:User

標籤:

在應用別人介面的時候,總是要用簽名,很是不理解簽名這是怎麼知道做的。通過對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];            }

 

C# Attribute應用:類簽名

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.