简体   繁体   English

我无法用自定义授权属性装饰整个Controller

[英]I can't decorate whole Controller with my Custom Authorize Attribute

I try to decorate a full controller class like this: 我尝试装饰一个完整的控制器类,如下所示:

namespace SisParkTD.Controllers
{
    [CustomAuthorize]
    public class AbonosController : Controller
    {

I can decorate the methods in the controller but can't decorate the full controller. 我可以装饰控制器中的方法,但不能装饰完整的控制器。

The error i get when i try to decorate the controller class is this: 我尝试装饰控制器类时得到的错误是这样的:

The attribute 'CustomAuthorize' is not valid on this declaration type. 属性“CustomAuthorize”在此声明类型上无效。 It is only valid on 'method' declarations. 它仅对'方法'声明有效。

Attribute 'SisparkTD.Filters.CustomAuthorizeAttribute' is not valid on this declaration type. 属性'SisparkTD.Filters.CustomAuthorizeAttribute'在此声明类型上无效。 Is is valid on 'Method' declarations only. 仅在“方法”声明中有效。

Here is the code for the CustomAuthorizeAttribute: 以下是CustomAuthorizeAttribute的代码:

namespace SisParkTD.Filters
{
    [AttributeUsage(AttributeTargets.Method)]
    public class CustomAuthorizeAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            using (var db = new SpContext())
            {
                var controller = (string)httpContext.Request.RequestContext.RouteData.Values["controller"];
                var action = (string)httpContext.Request.RequestContext.RouteData.Values["action"];
                var accion = db.Acciones.FirstOrDefault(a => a.Descripcion == action && a.Pagina.Descripcion == controller);
                var username = httpContext.User.Identity.Name;
                var usuario = db.Usuarios.FirstOrDefault(u => u.NombreDeUsuario == username);

                if (usuario == null) return false;
                if (accion == null) return false;
                var rolesId = db.RolesUsuarios.Where(ru => ru.UsuarioId == usuario.UsuarioId).Select(ru => ru.RolId);
                if (!rolesId.Any()) return false;

                foreach (var rolId in rolesId)
                {
                    if (db.Permisos.Find(rolId, accion.AccionId) != null) return base.AuthorizeCore(httpContext);
                }
                return false;
            }
        }
    }
}

Remove [AttributeUsage(AttributeTargets.Method)] from the attribute. 从属性中删除[AttributeUsage(AttributeTargets.Method)]。 (credit to SLaks for the answer) (感谢SLaks的回答)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果放入外部程序集,我无法使用自定义授权属性 - I cannot use my Custom Authorize attribute if put in an external assembly 我可以使用自定义属性类装饰方法来自动重试 - Can I decorate a method using custom attribute class to Auto re-try 为所有控制器的自定义策略和全局的操作设置authorize属性 - Set authorize attribute for Custom policy for all controller and Actions at global 自定义授权属性显示拒绝消息而不路由到控制器 - Custom Authorize Attribute show denied message without routing to controller 编写自定义[Authorize]属性 - Writing custom [Authorize] attribute WebApi自定义授权属性 - WebApi Custom Authorize attribute 我可以使用[Authorize(Role =“ Role”)]属性对每个角色进行自定义重定向吗? - Can I use [Authorize(Role = “Role”)] Attribute to do custom redirects per role? 为什么我的自定义路由与 AccountController / [Authorize] 属性发生冲突? - Why are my custom routings colliding with the AccountController / the [Authorize] attribute? 具有Authorize属性的控制器被调用两次 - Controller with Authorize attribute is called twice 部署到IIS后,自定义授权属性不起作用 - Custom authorize attribute doesn't work after deploying to IIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM