2

I'd like to customize System.Web.Http.AuthorizeAttribute class like this :

 public class MyAuthorizeAttribute : System.Web.Http.AuthorizeAttribute
    {

        public PermissionsEnum IsPermitted { get; set; }


        protected override bool IsAuthorized(HttpActionContext actionContext)
        {
            if (System.Web.HttpContext.Current.Session["Role"] == null) return false;
            string rol = (string)System.Web.HttpContext.Current.Session["Role"];

            if (rol == "Admin" || Roles == "Super Admin") IsPermitted = PermissionsEnum.Administration;
            else IsPermitted = PermissionsEnum.Collaboration;
            return base.IsAuthorized(actionContext);
        }
    }

  [Flags]
    public enum PermissionsEnum
    {
        Administration,
        Collaboration
    }

I used it in controller :

[MyAuthorizeAttribute(IsPermitted = PermissionsEnum.Administration)]
    public class PointageController : Controller
    {
        public ActionResult GraphesEtStatistiques()
        {
            return View();
        }
         [MyAuthorizeAttribute(IsPermitted = PermissionsEnum.Administration)]
        public ActionResult Pointage()
        {
            return View();
        }
        public ActionResult Parametrage()
        {
            return View();
        }
        public ActionResult GetMessages()
        {
            MessagesRepository _messageRepository = new MessagesRepository();
            return PartialView("_MessagesList", _messageRepository.GetAllMessages());
        }
    }

My problem is that I can access to the Pointage view even IsPermitted=PermissionsEnum.Collaboration !!!! .

So :

  1. What is the reason of this problem?
  2. How can I fix it?
1
  • 1
    It seems as you're always returning the return the value from the base class. Do you want to return IsPermitted == PermissionsEnum.Administration instead? Commented Feb 8, 2016 at 14:52

2 Answers 2

3
  1. What is the reason of this problem?

Your problem is that your logic within your IsAuthorize method is improper.

  1. How can I fix it?

...set a breakpoint and debug your IsAuthorized method.

From looking at the code you provided, with the way it is currently structured, the IsPermitted property is superfluous. You pass it into the attribute when decorating your controller, but then inside your IsAuthorized method, you do nothing with the injected value. Instead, you set it independently. Then you call the base AuthorizeAttribute's IsAuthorized method, and the base attribute has no concept of your enum.

I can't know for sure if this will solve your domain requirements, but this will at least give you a functional IsAuthorized method that you can build from:

protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        if (System.Web.HttpContext.Current.Session["Role"] == null) return false;
        string role = (string)System.Web.HttpContext.Current.Session["Role"];

        if ((role == "Admin" || role == "Super Admin") //recycling your condition
           && IsPermitted == PermissionsEnum.Administration) return true;

        if ((role == "Collaborator"
           && IsPermitted == PermissionsEnum.Collaborator) return true;

        return false;
    }
Sign up to request clarification or add additional context in comments.

Comments

1

Above all, if you set [MyAuthorizeAttribute(IsPermitted = PermissionsEnum.Administration)] over your controller(s), this means all actions implemented inside that class will use the same Authorization, even you set other Authorization for each method...

If you want to customize your authorization for each Action you must remove the attribute over all controller(s).

The Authorized Method :

protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        if (System.Web.HttpContext.Current.Session["Role"] == null) return false;
        string rol = (string)System.Web.HttpContext.Current.Session["Role"];

        var userPermittedFlag = (rol == "Admin" || rol == "Super Admin") ? PermissionsEnum.Administration : PermissionsEnum.Collaboration;
        return userPermittedFlag == this.IsPermitted;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.