Is it possible to create a Logon System with ASP.NET MVC but not use the MembershipProvider?

后端 未结 4 2075
耶瑟儿~
耶瑟儿~ 2020-11-30 16:35

I have an existing database with a users table, and we are planning to take the database and use it for a new system built in ASP.NET MVC. However, what I am uncertain about

4条回答
  •  借酒劲吻你
    2020-11-30 17:12

    Of course you can. I did it for my projects completely ignoring the membership provider.

    You need to implement your own ActionFilter. Basically, it will intercepts control before a controller action is hit. Inside of it you decide whether to continue on to the action or redirect the user to login page.

    For the attribute you can define any parameters you need to support your authentication/authorization model.

    public class AuthorizationAttribute : ActionFilterAttribute, IActionFilter
    {
       public MyRole UserRole { get; set; }
    
       void IActionFilter.OnActionExecuting (ActionExecutedContext filterContext)
       {
           // Decide whether to grant access to the action or redirect away
       }
    }
    
    [Authorization (UserRole = MyRole.All)]
    public class UserController : Controller
    {
        [Authorization (UserRole = MyRole.Admin)]
        public ActionResult Delete ()
        {
        }
    }
    

    Regarding the concerns expressed in the comments. Yes, enabling output cache will interfere with authorization. One just has to be aware of that.

    Explanation of the problem: ASP.NET MVC Tip #40 - Don’t Cache Pages that Require Authorization

提交回复
热议问题