C # Attribute application: Class signature,

Source: Internet
Author: User

C # Attribute application: Class signature,

When using other interfaces, you always need to use a signature, but you do not understand how the signature is implemented. Learn about Attribute. Generally, Attribute can be used for signature applications.

The specific process is as follows:

First, we need to define a class that inherits the Attribute. The main use of this class is the methods, parameters, and encrypted files used for signature.

1 public class CashiSongAttribute: Attribute 2 {3 /// <summary> 4 // signature parameter 5 /// </summary> 6 public string [] Param {get; set;} 7 // <summary> 8 // whether to sign 9 // </summary> 10 public bool IsSign {get; set ;} 11 /// <summary> 12 // encrypt the file 13 /// </summary> 14 /// <param name = "bp"> </param> 15 // /<param name = "mi"> </param> 16 // <returns> </returns> 17 public string ParamEncryption (BasePage bp, system. refle Ction. 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

Create a page, create a method on the page, and add this feature

1 [CashiSong (IsSign = true, Param = new string [] {"op", "name"})] 2 public string getceshicon () 3 {4 return "signature successful! "; 5}

The following key is to verify that the parameters are correct and the encrypted file is correct when you call the method again.

Create a base class BasePage. This base class is mainly responsible for accepting parameters, specifying methods specified by parameters, and determining whether the signature information is correct.

The Application of System. Reflection. MethodInfo is used here to obtain the Attribute parameter content.

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> /// signature judgment /// </summary> /// <param name = "mi"> </param> public void Attribute_Jude (MethodInfo mi) {MsgModel Msg = new MsgModel (); if (mi. isDefined (typeof (cashisongattried), false) {object [] attrs = mi. getCustomAttributes (typeof (cashisongattrites), False); CashiSongAttribute iplimit = (cashisongattrimit) 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 ;}}

Method for obtaining the content of a property parameter (CashiSongAttribute is a custom property class)

 if (mi.IsDefined(typeof(CashiSongAttribute), false))            {                object[] attrs = mi.GetCustomAttributes(typeof(CashiSongAttribute), false);                CashiSongAttribute iplimit = (CashiSongAttribute)attrs[0];            }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.