I know of a simple role provider in which, if I need to limit a specific action, I just need to write Authorize (Roles = "Admin") or I need to limit a certain part of the view to write @if (User. IsInRole ("Administrator")) .
But my question is, if my roles are not fixed and stored in the database, and my super-administrator can edit and delete them.
My requirement is that superadmin can add, update, delete roles, as well as create different users and support the roles of these users.
I did a lot of searches and found something as follows
[AttributeUsage (AttributeTargets.Method|AttributeTargets.Class,Inherited = true,AllowMultiple=true) ] public class CustomRole : AuthorizeAttribute { protected override bool AuthorizeCore(HttpContextBase context) { Respository db = new Respository(); if (db.UserMasters.Where(x => x.user_name == context.User.Identity.Name).Count() > 0) { return true; } else { return false; } } }
Here I can use this code to authorize an action method as follows
[CustomRole] public ActionResult Details(int id = 0) { Employee employee = db.Employees.Find(id); if (employee == null) { return HttpNotFound(); } return View(employee); }
Here my action method is protected, but what if I want to protect part of the view with this user-defined method. How to use this functionality to achieve functionality as User.IsInRole ("Admin")?
source share