Read a few documents on this, self-summary:
An attribute (Attribute) is an additional property description for a method or class, which is an additional description
Here is an example of my own copy of the program:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Reflection;namespacesometest{classProgram {Static voidMain (string[] args) {Demo D=Newdemo (); stringUsername ="Lucy"; MethodInfo mi= D.gettype (). GetMethod ("Test"); if(Mi = =NULL)return; Allowexecuteattribute att= Attribute.GetCustomAttribute (MI,typeof(Allowexecuteattribute)) asAllowexecuteattribute; if(Att = =NULL)return; if(Att. Check (username)) Console.WriteLine ("Allow execution"); ElseConsole.WriteLine ("Do not allow execution"); Console.readkey (); } } classDemo {[Allowexecute ("Jack,tom")] Public voidTest () {}}/// <summary> ///identifies the user that a method allows execution/// </summary> Public classAllowexecuteattribute:attribute {/// <summary> /// /// </summary> /// <param name= "Allowedusers" >A concatenated string of user names that are allowed to execute</param> PublicAllowexecuteattribute (stringallowedusers) { This. _allowedusers =allowedusers; } Private string_allowedusers; Public BOOLCheck (stringuserName) { return This. _allowedusers.tolower (). IndexOf (Username.tolower ()) >-1; } }}
Here are some ways to use the Authorizeattribute feature in MVC in the company project:
Public classSingleuserauthorize:authorizeattribute {[ValidateInput (false)] protected Override BOOLAuthorizecore (HttpContextBase HttpContext) {Hashtable Useronline= (Hashtable) (httpcontext.application["Online"]); if(Useronline! =NULL) {IDictionaryEnumerator IdE=Useronline.getenumerator (); stringStrkey =string. Empty; if(Useronline.count >0) { while(Ide.movenext ()) {//when you log in, determine if the session you saved is the same as the session on the current page if(Useronline.contains (HttpContext.Session.SessionID)) {if(Ide.key! =NULL&&idE.Key.ToString (). Equals (HttpContext.Session.SessionID)) {//determines whether the value saved by the current session is a logout value if(Ide.value! =NULL&&"XXXXXX". Equals (IdE.Value.ToString ())) {//Verify that logoff is cleared sessionUseronline.remove (HttpContext.Session.SessionID); HttpContext.Application.Lock (); httpcontext.application["Online"] =Useronline; HttpContext.Response.Write ("<script>top.location.href= '/home/message?message=offline ';</script>"); HttpContext.Response.End (); return false; } } } Else { return false; } } return true; } Else { return false; } } return false; } }
It feels like this is a violation of the nature of the design.