Question

I would like to extend the filter of keywords for Authorize Attribute without out creating an custom authorization object.

I know the build in keyword are Roles,Users. I would like to add a new keyword call level and see if the user access level is greater than certain level.

This would allow me to just have one roles created for user.

The usage of this would be

[Authorize(level > 1)]

Please let me know if you need additional information to make you understand better the issue i am trying to resolve.

I have looked but have not been able to resolve this issue. Can you provide a code sample? this is what i have so for. I am getting error when i use the level filter.

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);
    }
}
Was it helpful?

Solution

This is not possible to just ADD new filters to the built-in AuthorizeAttribute. You will need to create a new attribute that inherits from AuthorizeAttribute.

There a ton of blog posts and questions on SO about creating custom AuthorizeAttributes.

EDIT: Ok, good start. Your issue is that you can't do Level > 1 like you are trying. Assuming you are try to say that you what auth to pass when the users Level is greater than 1, you can change your property in the attribute to indicate this.

E.g.

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top