I have a PermisoAttribute class that is inherited from an ActionFilterAttribute, which I show below.
public class PermisoAttribute : ActionFilterAttribute
{
public string username { get; set; }
public int ProgramId { get; set; }
public int ModuleId { get; set; }
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if (!permisos(this.username,this.ProgramId,this.ModuleId))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
{
controller = "Home",
action = "Denegado"
}));
}
}
}
How can you observe this take as parameter if the user has or not permissions to know if the user can execute the action, invoking a class permissions that receives a username, programaid, ModuleId as parameters and returns true or false as the user has or not permissions.
Here's how I intend to use my PermisoAttribute class
[HttpGet]
[Permiso(username = User.Identity.Name, ModuleId = 3, ProgramId = 2)]
public ActionResult Transactions()
{
// codigo de la accion.
}
but I receive this error to use the code User.Identity.Name
Error CS0120 An object reference is required for the non-static field, method, or property 'Controller.User'
What I want is to get the name of the current user to send it and see if that user has permission or not.