ASP. net mvc uses ActionFilterAttribute to determine whether a user is logged on.

Source: Internet
Author: User

ActionFilterAttribute is an Action filtering class that is executed before an action is executed. actionFilterAttribute is a VC class that specifically processes action filtering. based on this principle, assuming an action: AdminIndex, the minimum access requirement for this Action is that the user must be logged on. then, we need to add a judgment public ActionResult AdminIndex () {return View ();} on the [CheckLogin ()] // above the Action ();} // end AdminIndex as mentioned above, ActionFilterAttribute is the filter base class, so CheckLogin () determines that it must inherit ActionFilterAttribute as follows: public class CheckLoginAttribute: ActionFilterAttribute {pub Lic CheckLoginAttribute () {}} // end class CheckLoginAttribute has an abstract in the ActionFilterAttribute base class when public abstract void OnActionExecuting (ActionExecutingContext filterContext ); as the name implies, it refers to what to do before an Action is executed. after the CheckLoginAttribute inherits the main class, the OnActionExecuting (..) for example, public override void OnActionExecuting (ActionExecutingContext filterContext) {}// end Action, we can write the processing we want to do in this method. for example, public override void OnActi OnExecuting (ActionExecutingContext filterContext) {// if the user has not logged on to if (! UserLogin. isLogined) {// processing method ErrorRedirect (filterContext); return;} // end if} www.2cto.com // error handling method private void ErrorRedirect (ActionExecutingContext filterContext) {filterContext. result = new RedirectToRouteResult ("Default", new RouteValueDictionary (new {controller = "Home", action = "Default"});} // end ErrorRedirect when the ErrorRedirect method is executed, it selects the specified Route and jumps to the Action of a non-AdminIndex.

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.