how authorization asp(mvc) project from controller?

前端 未结 2 528
半阙折子戏
半阙折子戏 2021-01-29 02:49

I\'m new in asp and I created a login page for my web project but and I set authentication but I can not set authorization for my project! I saw many links like this Authenticat

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 03:45

    If you want authorization on your whole controller, just set the Authorize attribute on your controller:

    [Authorize]
    public class AuthenticationController : Controller
    {
    
    }
    

    If you want authorization on a sigle action:

    public class AuthenticationController : Controller
    {
        [Authorize]
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome, " + HttpContext.User.Identity.Name;
        }
    }
    

    EDIT: Only authenticated users will be able to navigate through authorized methods or controllers

提交回复
热议问题