How to get the current user name to send it to a PermitAttribute as Parameter

1

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.

    
asked by Elvin Acevedo 28.03.2017 в 23:31
source

1 answer

1

First of all, the error that is marking you is because you can not use variables in the attributes, that is, you only have to handle "hard" static values, for example:

[Permiso(username = "Admin", ModuleId = 3, ProgramId = 2)]

Second, you do not need to set the user name in an authorization attribute, since that information is already stored in the session data, all you have to do is call it from within the attribute with filterContext.HttpContext.User.Identity.Name :

public class PermisoAttribute : ActionFilterAttribute
{
    public int ProgramId { get; set; }
    public int ModuleId { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        base.OnActionExecuting(filterContext);

        if (!permisos(filterContext.HttpContext.User.Identity.Name, this.ProgramId, this.ModuleId))
        {
            filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new
            {
                controller = "Home",
                action = "Denegado"
            }));
        }
    }
}

As a final comment, it is not necessary to place the attributes like this:

[HttpGet]
[Permiso(ModuleId = 3, ProgramId = 2)]

but you can add the ones you need with commas, for example:

[HttpGet, Permiso(ModuleId = 3, ProgramId = 2)]
    
answered by 30.03.2017 / 21:49
source