asp.net mvc - Checking object access using ActionFilters -
I am using the BaseController class with a custom mechanism, overriding overting () I use this base for all my controllers The controller class is used as a convenient way to check the id route value when it is passed to the controller.
This has worked fine ... I check an ID value, and if it is present then I check to see the name of the controller which determines the type of object that is from ID Is related.
But then ... I have started getting errors, when a partial view is called in controller B and the same ID is introduced in the filtercontext, which is used for the parent controller / view it was done. For example, say I have a URL of local host / project / description / 5 where the project is controller and action has been taken with id = 5. The controller "Notes" is called due to partial view and this time my mechanism has begun for controller "Notes" and the original "Project" ID. The user is allowed access to project id = 5, but note Id = 5 not
I hope this makes sense - I am struggling to explain. Now I need to find a way to either ignore a partial view, or to find a way to handle it, or to use a completely different approach. My ultimate goal is to check object access in my applications based on an object's ID. Have to be Is there a "best approach" for this kind of problem? Maybe I have to write a filter for each controller? Or maybe you can also check the action name, so I know when to "ignore" ID?
My Base Controller ...
Public class AccessCheckAttribute: ActionFilterAttribute {Public Override Zero On-Offing Actioning (Action Exposing Contact Filter Context) {int id; If (filterContext.RouteData.Values ["id"]! = Null) {if (int.TryParse (filterContext.RouteData.Values ["id"]. ToString (), ID out)) {SPTdb DB = New SPTdb (); Switch (filterContext.RouteData.Values ["Controller"]. ToString (). ToLower ()) {case "project": project = db.Projects.Find (id);
To identify which institution is very problematic with your ID, your current problem Like you will have many complications. You can use a common base class to avoid it, it will still be a problematic approach
In addition to this, if you spread your database access logic everywhere, Can not use caching capabilities For example: If the query contains a valid ID in the query for your query and you want to show the use of the actual reference, ask the same entity in the controller with a different reference. In your case, this can not be the cause of any problem, but it is still a very unnecessary thing.
To solve your problem, you should get rid of the action filter and use a simple structure such as the repository pattern:
Public Sector Project Controller: Controller {Private Generic Read & lt; Project & gt; _projects; // You can definitely use DE Public Project Controller () {_projects = new ....} Public View Result Details (id) {var project = _projects.Get (id); If return (project == faucet) see return (;); ....}
Comments
Post a Comment