自定义授权过滤器属性参数

Bil*_*lla 4 c# asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

我有以下要求来实现访问控制列表

public class SecurityObject{
public string Key{get;set;}
public string DisplayName{get;set;}
public bool isAllowed{get;set;}
}

public class Role{
List<SecurityObject> AccessibleObjects{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

目前我使用表单身份验证进行基本授权.以下是我的代码

的Global.asax.cs

 public class MvcApplication : System.Web.HttpApplication
 {
    public override void Init()
    {
        this.PostAuthenticateRequest += new 
                           EventHandler(MvcApplication_PostAuthenticateRequest);

        base.Init();
    }
    void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
    {
      HttpCookie authCookie =
       HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
        if (authCookie != null)
        {
            string encTicket = authCookie.Value;
            if (!String.IsNullOrEmpty(encTicket))
            {
                FormsAuthenticationTicket ticket = 
                                 FormsAuthentication.Decrypt(encTicket);

                string[] userData = ticket.UserData.Split(new string[] { "___" },
                                 StringSplitOptions.None);
                string[] roles = null;
                if (userData.Length > 1)
                {
                    roles = userData[1].Split(',');
                }
            MyCustomIdentity identity = new MyCustomIdentity(ticket);
            GenericPrincipal principle = new GenericPrincipal(identity, roles);
            HttpContext.Current.User = principle;
            }
        }
    }}
Run Code Online (Sandbox Code Playgroud)

我目前的控制器类

public class AdminController : Controller
 {
  [HttpPost, Authorize, ValidateAntiForgeryToken]
    public ActionResult SaveUser(UserDetailViewModel viewModel)
    {

    }
  }
Run Code Online (Sandbox Code Playgroud)

我的目标控制器类

public class AdminController : Controller
 {
  [HttpPost, Authorize(ACLKey="USR_SAVE"), ValidateAntiForgeryToken]
    public ActionResult SaveUser(UserDetailViewModel viewModel)
    {

    }
  }
Run Code Online (Sandbox Code Playgroud)

我希望我的action方法用ACLKey修饰,我想检查用户角色是否具有给定的密钥,并根据我需要执行或返回HttpUnauthorizedResult页面,即使是来自jQuery的Ajax请求.

在ASP.NET MVC中提到了许多像自定义授权但我没有找到执行表单身份验证和自定义ACLKey检查的方法.

如何USR_SAVE使用 CustomAuthorizeFilter 解析值并处理自定义身份验证

Mur*_*san 5

你可以这样试试

public class FeatureAuthenticationAttribute : FilterAttribute, IAuthorizationFilter
{
    public string AllowFeature { get; set; }

    public void OnAuthorization(AuthorizationContext filterContext)
    {

        var filterAttribute = filterContext.ActionDescriptor.GetFilterAttributes(true)
                                .Where(a => a.GetType() == 
                               typeof(FeatureAuthenticationAttribute));
        if (filterAttribute != null)
        {
            foreach (FeatureAuthenticationAttribute attr in filterAttribute)
            {
                AllowFeature = attr.AllowFeature;
            }
       List<Role> roles = 
       ((User)filterContext.HttpContext.Session["CurrentUser"]).Roles;
       bool allowed = SecurityHelper.IsAccessible(AllowFeature, roles);
         if (!allowed)
            {
                filterContext.Result = new HttpUnauthorizedResult();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的行动方法

    [FeatureAuthentication(AllowFeature="USR_SAVE")]
    public ActionResult Index()
    {
    }
Run Code Online (Sandbox Code Playgroud)

希望对你有帮助!


Bri*_*ins 1

您可以使用过滤器属性:

public class ACLCheckAttribute : FilterAttribute, IActionFilter
Run Code Online (Sandbox Code Playgroud)

在OnActionExecuting中,您可以获取USR_SAVE。在不知道它来自哪里的情况下,我会假设它来自:

  • 表单:您可以通过导航到 HttpContext.Request.Form 集合,从传递到 ONActionExecuting 的上下文中获取任何表单值
  • Session等:HttpContext也会有这些。
  • 操作方法:从属性中,使用为操作传入的上下文,它有一个可以像字典一样访问的 ActionParameters 列表,允许您检查和提取您的值

如果有其他地方,请评论在哪里。您可以将此属性应用于控制器或方法,或者通过将其添加到 globalfilters 集合 (GlobalFilters.Filters.Add()) 或 App_Start 文件夹中的 FilterConfig 文件中来全局设置它。