In the exploration of. NET Single sign-on, a knowledge point is used: filter. Several common authentication:Authorization Filters, verify that the user has access to the page, Action Filter, verify that user information is present when users log in, exception handling, such as the session expires can return to the login page and so on.
First, what does the filter contain?
Filter is divided into four main types, spread over MVC, from page request to authorization, to access resources at all stages.
Authorization filters: Actions related to handling validation processing
Action filters: The start and end of action processing in the controller do intercept operations
Result filters: Processing before and after the view is rendered
Exception Filters: If an exception occurs in a request that adds a Exception filter, it will be intercepted.
Here is the order in which filter works in MVC:
Second, the filter of the small demo
Before the subsystem login permission, the filter must be filtered to see if the local cookie value can be checked for verification. The following is the code in filter:
public class Membervalidationattribute:authorizeattribute {public override void Onauthorization ( AuthorizationContext filtercontext) { //Read user login rights and information var membervalidation = FilterContext.HttpContext.Request.Cookies.Get ("Selfuserinfo"); If it is empty, it jumps to the login page, and if it is not empty, it returns the first requested page if (membervalidation = = null) { filtercontext.result=new Redirecttorouteresult (New RouteValueDictionary (new {controller= "account", aciton= "LogOn" }); return; } return; } }
The subsystem only needs to hit a [membervalidation] on the ActionResult to access the filter at login time.
[Membervalidation] Public ActionResult ChangePassword () { return View (); }
third, feel
A knowledge only for others to speak out, and understand, is the real learning. Don't think about all the things so hard, just do it.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
. NET Single Sign-on filters (filter)--"SSO"