How to restrict access to certain actions in controller in ASP.net MVC

前端 未结 4 721
甜味超标
甜味超标 2020-12-29 16:33

I am new to ASP.net MVC and created my first web application using it. In my application I am using database authentication. I have created Login action in controller which

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-29 16:55

    you should create a basecontroller and inherit other controlers from base controller and then check whether the session is null or not to authenticate users.

     public class BaseController : Controller
     {
            protected override void OnActionExecuting(ActionExecutingContext filterContext)
            {
                if (Session["User"]== null)
                {
                   filterContext.HttpContext.Response.Redirect("/somepage");
            }
    
     }
    
    public class SomeController : BaseController
    {
    
    }
    

提交回复
热议问题