C # attribute application: Class signing

Source: Internet
Author: User

In the application of other people's interface, always use the signature, very do not understand the signature how to know how to do. Learn by learning about the attribute. You can use attribute to do signature applications.

The process is as follows:

First we need to define a class that inherits attribute. The main use of this class is that the signature needs to use the method, parameters and get encrypted files

1   Public classCashisongattribute:attribute2     {3         /// <summary>4         ///Signature Parameters5         /// </summary>6          Public string[] Param {Get;Set; }7         /// <summary>8         ///whether to sign9         /// </summary>Ten          Public BOOLissign {Get;Set; } One         /// <summary> A         ///Encrypting Files -         /// </summary> -         /// <param name= "BP" ></param> the         /// <param name= "Mi" ></param> -         /// <returns></returns> -          Public stringparamencryption (basepage bp,system.reflection.methodinfo mi) -         { +             if(Param! =NULL&& param.length >0) -             { +                 stringMD5 ="op"+mi. Name.tolower (); A                 foreach(stringIteminchParam) at                 { -                     if(item. ToLower () = ="op"|| Item. ToLower () = =" Sign") -                         Continue; -MD5 + = Item +BP. Getrequest (item); -                 } -                 byte[] Bytestr =Encoding.Default.GetBytes (MD5); inMD5 _MD5 =NewMD5CryptoServiceProvider (); -                 byte[] Bytesend =_md5.computehash (BYTESTR); to                 returnBitconverter.tostring (Bytesend). Replace ("-",""); +             } -             return ""; the         } *}
View Code

Create a new page that creates a method on the page and adds the attribute

1[Cashisong (issign =true, Param =New string[] {"op","name" })]2          Public stringGetceshicon ()3         {4             return "Signature Successful! ";5}

The following key is again through the invocation of the time, verify that the parameters are consistent, the encryption file is correct.

Creates a base class BasePage, which is primarily responsible for accepting parameters, specifying the method specified by the parameter, and judging whether the signature information is correct.

This will be used: System.Reflection.MethodInfo application, get features attribute parameter content.

   Public classBasepage:page { PublicBasePage () { This. Load + =NewEventHandler (basepage_load); }        voidBasepage_load (Objectsender, EventArgs e) {Response.AddHeader ("Accept-charset","UTF-8"); stringOP = 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 voidattribute_jude (MethodInfo mi) {msgmodel Msg=NewMsgmodel (); if(MI.) IsDefined (typeof(Cashisongattribute),false))            {                Object[] Attrs = mi. GetCustomAttributes (typeof(Cashisongattribute),false); Cashisongattribute Iplimit= (Cashisongattribute) attrs[0]; ObjectResponsestr=NULL; if(Iplimit! =NULL&& Iplimit. Param.length >0)                {                    stringServer_sign = Getrequest (" Sign"); stringClient_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 voidSend (Msgmodel Msg) {Response.AddHeader ("Content-type","Applictaion/json"); JavaScriptSerializer JavaScript=NewJavaScriptSerializer (); stringCon =javascript.serialize (MSG);        Response.Write (Con); }         Public stringGetrequest (stringkey) {            if(Request.querystring[key] = =NULL)                return ""; Else                returnRequest.querystring[key]; }    }     Public classMsgmodel { Public stringmsg {Get;Set; }  Public inttoile {Get;Set; } }

method to get the contents of an attribute parameter (Cashisongattribute is a custom attribute class)

if (MI.) IsDefined (typeoffalse))            {                object[] attrs = mi. GetCustomAttributes (typeoffalse);                 = (cashisongattribute) attrs[0];            }

C # attribute application: Class signing

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.