ASP. net mvc uses ActionFilterAttribute for permissions.

Source: Internet
Author: User

ActionFilterAttribute is an Action filtering class that is executed before an action is executed. ActionFilterAttribute is a class of MVC that specifically processes action filtering. Based on this principle, we create a permission restriction.

For example, how to access test action in HomeController

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using MvcTest. Models;
Namespace MvcTest. Controllers
{
Public class HomeController: Controller
{
//
// GET:/Home/


Public ActionResult Index ()
{

Return View ();
}
[AuthorizeFilter]
Public ActionResult test ()
{
Return Content ("authorized to access ");
}
}
}


The AuthorizeFilterAttribute. cs content is as follows:

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;

Namespace MvcTest. Models
{
/// <Summary>
/// Permission Interception
/// </Summary>
[AttributeUsage (AttributeTargets. Class | AttributeTargets. Method, AllowMultiple = false)]
Public class AuthorizeFilterAttribute: ActionFilterAttribute
{
FilterContextInfo fcinfo;
// OnActionExecuted is called by the ASP. net mvc framework after the operation method is executed.
// OnActionExecuting is called by the ASP. net mvc framework before the operation method is executed.
// OnResultExecuted is called by the ASP. net mvc framework after the operation result is executed.
// OnResultExecuting is called by the ASP. net mvc framework before the operation result is executed.

/// <Summary>
/// It is called by the ASP. net mvc framework before the operation method is executed.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnActionExecuting (ActionExecutingContext filterContext)
{

Fcinfo = new filterContextInfo (filterContext );
// Fcinfo. actionName; // obtain the Domain Name
// Fcinfo. controllerName; get the controllerName

Bool isstate = true;
// Islogin = false;
If (isstate) // if yes
{
// Logic code
// FilterContext. Result = new HttpUnauthorizedResult (); // jump to the login page from the URL
// FilterContext. Result = new RedirectResult ("http://www.baidu.com"); // You can also jump to another site
// FilterContext. Result = new RedirectToRouteResult (new System. Web. Routing. RouteValueDictionary (new {Controller = "product", action = "Default "}));
}
Else
{
FilterContext. Result = new ContentResult {Content = @ "sorry, you do not have the permission for the current operation! "}; // Return Content directly (" sorry, you do not have permission for the current operation! ")
}

}
/// <Summary>
/// After the operation method is executed, it is called by the ASP. net mvc framework.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnActionExecuted (ActionExecutedContext filterContext)
{

Base. OnActionExecuted (filterContext );
}

/// <Summary>
/// OnResultExecuted is called by the ASP. net mvc framework after the operation result is executed.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnResultExecuted (ResultExecutedContext filterContext)
{
Base. OnResultExecuted (filterContext );
}
/// <Summary>
/// OnResultExecuting is called by the ASP. net mvc framework before the operation result is executed.
/// </Summary>
/// <Param name = "filterContext"> </param>
Public override void OnResultExecuting (ResultExecutingContext filterContext)
{
Base. OnResultExecuting (filterContext );
}

}

Public class filterContextInfo
{
Public filterContextInfo (ActionExecutingContext filterContext)
{
# Region
// Obtain the Domain Name
DomainName = filterContext. HttpContext. Request. Url. Authority;

// Obtain the module name
// Module = filterContext. HttpContext. Request. Url. Segments [1]. Replace ('/', ''). Trim ();

// Obtain the controllerName
ControllerName = filterContext. RouteData. Values ["controller"]. ToString ();

// Obtain the ACTION name
ActionName = filterContext. RouteData. Values ["action"]. ToString ();

# Endregion
}
/// <Summary>
/// Obtain the Domain Name
/// </Summary>
Public string domainName {get; set ;}
/// <Summary>
/// Obtain the module name
/// </Summary>
Public string module {get; set ;}
/// <Summary>
/// Obtain the controllerName
/// </Summary>
Public string controllerName {get; set ;}
/// <Summary>
/// Obtain the ACTION name
/// </Summary>
Public string actionName {get; set ;}

}

}

Download DEMO
  

 

 

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.