For decades, various authentication and authorization methods have been used, and the following methodology is used in my current MVC application.
Claims are used for all authorizations. Users are assigned one role (several roles are possible, but I do not need it) - more details below.
Typically, the ClaimsAuthorize attribute class is used. Since most of the controller actions are CRUD, I have a routine in the database generation with the first code that repeats all the controller actions and creates ticket types for each attribute of the Read / Edit / Create / Delete controller action. For example. of,
[ClaimsAuthorize("SomeController", "Edit")] [HttpPost]
For use in MVC View, the base class of the controller represents the elements of the view package.
protected override void OnActionExecuting(ActionExecutingContext filterContext) { // get user claims var user = filterContext.HttpContext.User as System.Security.Claims.ClaimsPrincipal; if (user != null) { // Get all user claims on this controller. In this controler base class, [this] still gets the descendant instance type, hence name List<Claim> claims = user.Claims.Where(c => c.Type == this.GetType().Name).ToList(); // set Viewbag with default authorisations on this controller ViewBag.ClaimRead = claims.Any(c => c.Value == "Read"); ViewBag.ClaimEdit = claims.Any(c => c.Value == "Edit"); ViewBag.ClaimCreate = claims.Any(c => c.Value == "Create"); ViewBag.ClaimDelete = claims.Any(c => c.Value == "Delete"); } base.OnActionExecuting(filterContext); }
For the website menu and other non-controller actions, I have other complaints. For example. whether the user can view a specific money field.
bool UserHasSpecificClaim(string claimType, string claimValue) { // get user claims var user = this.HttpContext.User as System.Security.Claims.ClaimsPrincipal; if (user != null) { // Get the specific claim if any return user.Claims.Any(c => c.Type == claimType && c.Value == claimValue); } return false; } public bool UserHasTradePricesReadClaim { get { return UserHasSpecificClaim("TradePrices", "Read"); } }
So where do the roles fit in?
I have a table that associates a role with (by default) a set of requirements. When you configure user authorization by default, the user receives assertions about his role. Each user may have more or fewer claims than the default. To simplify editing, the list of claims is shown by the controller and actions (in a row), and then other claims are listed. Buttons are used with a small amount of Javascript to select a set of actions to minimize the “clicks” required to select statements. When saving applications, users are deleted, and all selected applications are added. The web application only downloads applications once, so any changes should cause this static data to reload.
Therefore, managers can choose which statements belong to each role and which statements the user has after setting the role and default statements. The system has a small number of users, so managing this data is simple