First, what is filter
The filter filters in the ASP.NETMVC mode is a declarative programming method that supports four types of filters, each of which is: Authorization (authorization), Action (behavior), result (results), and exception (exception).
filter type |
interface |
descriptive narration |
authorization |
Iauthorizationfilter |
This type (or filter) is used to restrict a behavior method that enters a controller or controller |
exception |
Iexceptionfilter |
Used to specify a behavior that the specified behavior handles a behavior method or exception thrown in a controller |
action |
Iactionfilter |
Processing | before or after entering the behavior
Result |
Iresultfilter |
Used to return the previous or subsequent processing of the result |
However, there are only three filters that implement them by default, each of which is actionfilter (method), authorize (authorization), HandleError (Error handling). Various information such as those seen in the following table:
Filter filters |
Class name |
Implementing interfaces |
Descriptive narrative |
actionfilter |
Authorizeattribute |
Iauthorizationfilter |
This type (or filter) is used to restrict a behavior method that enters a controller or controller |
handleerror |
Handleerrorattribute |
Iexceptionfilter |
Used to specify a behavior that the specified behavior handles a behavior method or exception thrown in a controller |
Define Yourself |
ActionFilterAttribute |
Iactionfilter and Iresultfilter |
before the method runs / after the processing. returns the previous or subsequent processing of the result. |
the third kind of filter that you define, you must inherit ActionFilterAttribute .
It is the base class ActionFilterAttribute provided by Asp.netmvcframework. This class implements the Iactionfilter and Iresultfilter interfaces. ActionFilterAttribute There are several ways to rewrite:
-
OnActionExecuting |
in the Controller Action called before running |
onactionexecuted |
controller action call | after running
onresultexecuting |
controller action result call | before running
onresultexecuted |
in the controller Action Result called after running |
Each page is implemented to verify that the user information is stored in a cookie. Expired user information will expire and jump to the login page.
The general idea is this: Save the user information from the page in the login controller to the cookie and set the cookie expiration time. A method run in each controller runs the filter first. Check to see if the cookie is a real user information.
Second, practice
The first thing to do is to save the username information in a cookie. Create a cookie in the controller that is logged in. A cookie is a key-value pair pattern (key, value).
#region Save username in a cookie///<summary>/// Save cookies//</summary>/ <returns ></returns> public void Createcookie ()//This action voluntarily writes the login information to the cookie { HttpCookie UserName = new HttpCookie ("name"); Username.value = request["UserName"]; System.Web.HttpContext.Current.Response.SetCookie (UserName); Cookie save time username.expires = DateTime.Now.AddHours; } #endregion
Next, create your own defined in the filter loginfilter, check whether cookies are useful for household information:
When both classes and methods are used, this feature is added at this time, without adding. Only method works [AttributeUsage (AttributeTargets.All, AllowMultiple = true, inherited = True)] public class Loginfilter:actionfilterattribute {//<summary>// onactionexecuting is the action before operation// </summary>// <param name= "Filtercontext" ></param> public override void OnActionExecuting (ActionExecutingContext filtercontext) { //infer Cookieusernamepassword exists HttpCookie cookiename = System.Web.HttpContext.Current.Request.Cookies.Get ("name"); if (cookiename = = null) { Filtercontext.result = new Redirectresult ("/login/index") ; } }}
Finally, you have to lay the label of the filter that you define in each controller
<span style= "FONT-SIZE:18PX;" > [loginfilter] public class Homecontroller:controller {public actionresult Index () { Viewbag.message = "Welcome to ASP. mvc!"; return View (); } </span>
The controller is labeled, the bottom of all the methods before the operation will run the filter first, the implementation of filtering. Can be based on their own business. Adjust the label, or use global globally.
This time the filter study, exposed one of my shortcomings, debugging bugs, encountered the red wavy line Ctrl + Z undo, rather than to seriously look at the problem, solve the problem. Self-Test!
ASP. NET MVC Filter Authentication User Login