IAuthorizationFilter study notes (access control)

Step One: Create a class that implements the interface CheckLoginFilter IAuthorizationFilter. Please note that the interface is located namespace using System.Web.Mvc;

public void OnAuthorization(AuthorizationContext filterContext)
{
string controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
string actionName = filterContext.ActionDescriptor.ActionName;
if(controllerName=="Login"&&(actionName.ToUpper()=="index".ToUpper() || actionName.ToUpper() == "login".ToUpper()))
{

}
The else
{
// check the login status
IF (filterContext.HttpContext.Session [ "username"] == null)
{
the ContentResult the ContentResult new new = CON ();
con.Content = "not landed";
filterContext.Result = CON;
}
}
}

A second step; adding to the filter in Global.asax

GlobalFilters.Filters.Add(new CheckLoginFilter());

If the code shown above, the session is not detected, i.e., with no logs in and returns a string. The same can be realized jump to the landing page when the user is not logged in. Code as follows:

//ContentResult con = new ContentResult();
//con.Content = "没有登陆";
RedirectResult red = new RedirectResult("/login/index");
filterContext.Result = red;

In fact, the use of class RedirectResult.

Guess you like

Origin www.cnblogs.com/yagamilight/p/12217912.html