Action Validation login in MVC (ActionFilterAttribute)

Source: Internet
Author: User

Method One: 1. Create a Global action filter (register filters in AppStart's filterconfig. ADD (New Loginattribute ());)

2. Contoller or action that does not require login

The filter attribute is added to the class or action (Isneed=false)

Method Two: 1. Create a filter that is not registered globally

2. Create a Basecontroler, and then add the filter attribute Basecontroller top

3. Login required to inherit the Basecontroller, do not need to log on to inherit the Basecontroller

Add: If you do not want to build basecontroler, how can be directly on the controller controllers or the action method to add a custom filter

Note: In 1.OnActionExecuting, base. OnActionExecuting (Filtercontext);

If there is more than one filter in the current project, add base. OnActionExecuting (Filtercontext);

No additional filter is performed without adding

2.filtercontext.result = new Redirectresult ("/user/login");

In the filter inside the page jumps with filtercontext.result = new Redirectresult ("/user/login");

If using FilterContext.HttpContext.Response.Redirect ("/user/login"); After the jump, you will continue to perform the action behind it.

Eg:home/index Jump User/login, will continue to execute index/action inside the method

Action Validation login in MVC (ActionFilterAttribute)

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.