Verify the logon and permissions in MVC, and verify the logon permissions in MVC.

Source: Internet
Author: User

Verify the logon and permissions in MVC, and verify the logon permissions in MVC.

Using mvc as the background management system requires logon and permission judgment. Here the ActionFilterAttribute feature of mvc is used.

The details are as follows:

Declare a CheckUser class that inherits the ActionFilterAttribute class

Public class CheckUserFilter: ActionFilterAttribute
{
Public override void OnActionExecuting (ActionExecutingContext filterContext)
{
Base. OnActionExecuting (filterContext );
If (filterContext. HttpContext. Request. Cookies ["backUser"] = null)
{
FilterContext. HttpContext. Response. Redirect ("~ /Home/Login "); // jump to the logon page for Logon
}
Else
{
String controllerName = filterContext. RouteData. Values ["controller"]. ToString ();
BackLogin blogin = new BackLogin ();
Bool l = blogin. IsLogin (); // verify the information in cookies again to prevent forgery
If (l = false)
{
FilterContext. HttpContext. Response. Redirect ("~ /Home/Login ");
}
Else
{
Bool B = blogin. IsHavaRights (controllerName); // verify the permission
If (B = false)
{
FilterContext. HttpContext. Response. Redirect ("~ /Home/NoRight ");
}
}
}
}

}

The next step is simple. You only need to add verification on the action to be verified.

[CheckUser]public ActionResult Index(){       return View();  }

If you do not log on, or you do not have the required permissions, the page is displayed.

Of course, other verification features such as the AuthorizeAttribute class exist in mvc. Specific use methods, a lot of online.

 

Indicate the source for reprinting.

Related Article

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.