Authorize Attribute extentions new keyword



我想在不创建自定义授权对象的情况下扩展Authorize Attribute的关键字过滤器。

我知道内置关键字是Roles,Users。我想添加一个新的关键字调用级别,看看用户访问级别是否大于某个级别。

这将允许我只为用户创建一个角色。

这将是

[Authorize(level > 1)]

如果你需要更多的信息来让你更好地理解我试图解决的问题,请告诉我。

我看了看,但没能解决这个问题。你能提供一个代码示例吗?这就是我这么做的目的。我在使用级别过滤器时出错。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
[Auth(Level > 1)]
public ActionResult Index()
{
}
public class Auth : AuthorizeAttribute
{
    public int Level { get; set; }
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
      base.OnAuthorization(filterContext);
    }
}

这不可能只在内置的AuthorizeAttribute中添加新的过滤器。您将需要创建一个从AuthorizeAttribute继承的新属性。

SO上有很多关于创建自定义AuthorizeAttributes的博客文章和问题。

编辑:好的,好的开始。你的问题是你不能像现在这样做Level > 1。假设您试图告诉您当用户Level大于1时要传递什么身份验证,那么您可以更改属性中的属性来指示这一点。

例如

public class Auth : AuthorizeAttribute
{
    public int MinLevel { get; set; }
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        //This is where you will need to determine if the user is authorized based upon the minimum level.
        base.OnAuthorization(filterContext);
    }
}

最新更新