简体   繁体   中英

How to provide access of action methods for particular user in a controller in asp.net mvc

Here, suppose we have two users User1 and User2, and there are two action methods in controller,

  public class HomeController:Controller
   {

     public ActionResult Method1()
     {
       return View();
     }

     public ActionResult Method2()
     {
       return View();
     }
   }

For the user1, i want to provide access both the methods, and for the user2, i want to provide access method1 only.

How it is Possible in MVC? Please help me anyone?

You can use Authorize attribute and can decorate your code as below :-

public class HomeController:Controller
   {

     public ActionResult Method1()
     {
       return View();
     }

     [Authorize(Users="user1")]
     public ActionResult Method2()
     {
       return View();
     }
   }

For more details :-

http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM