How can I accomplish this using MVC.NET? I've found resources on the Internet about how to do it in ASP.NET, but I haven't found a way to do it in MVC.NET (I'm an MVC.NET newbie, though). I can generate a 401 response using [Authorize] attributes but I don't have any idea about what to do after that.
[EDIT] After some playing around, I figured that if I can find a way to force the application to reauthenticate itself and bring up the login dialog, I may be able to do this. I think that if I set the Auth开发者_如何学Pythonorization attribute to something like [Authorize(Users="NooneAllowed")] it should bring up the login dialog. Is there another way to force it to reauthenticate, because the above hack is pretty much useless as it will still deny access.
You have to asign role for each user on time of registration by using the below code
Roles.CreateRole(RoleName);
Roles.AddUserToRole(UserName, RoleName);
Role name as "Admin" or "User" or "Employee" or "Customer" or what ever you wish it may. You should done it on the time of registration.
Then you define the like this to differentiate the using for users. If you need Admin only to take access the below method, then you have to define the role as "Admin"
[HttpGet]
[Authorize(Roles = "Admin")]
public ActionResult ViewInventoryList()
{
List<Inventory> inventoryList = new List<Inventory>();
inventoryList = connectDatabase.SelectInventory();
return View("ViewInventory", inventoryList);
}
If you need User only to take access the below method, then you have to define the role as "User"
[HttpGet]
[Authorize(Roles = "User")]
public ActionResult ViewInventoryList()
{
List<Inventory> inventoryList = new List<Inventory>();
inventoryList = connectDatabase.SelectInventory();
return View("ViewInventory", inventoryList);
}
精彩评论