0

In my ASP.NET MVC 4 controller class I have actions that I decorate with CustomAuthorize attribute so the access is restricted to some roles.

I would like to get the roles from inside the action methods and for this I need the CustomAuthorize attribute that the method is decorated with.

How do I do this?

Sample code of what I am trying to do is below:

public class TestController : Controller
{
    // Only the users in viewer and admin roles should do this
    [CustomAuthorize("viewer", "admin")]
    public ActionResult Index()
    {
        //Need CustomAuthorizeAttribute so I get the associated roles
    }
}

CustomAuthorizeAttribute is a subclass of System.Web.Mvc.AuthorizeAttribute.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
user3885927
  • 3,363
  • 2
  • 22
  • 42

3 Answers3

2

Attributes are not per instance, they are per class so there is no "current" instance of the CustomAuthorizeAttribute. See the documentation on attributes.

https://msdn.microsoft.com/en-us/library/z0w1kczw.aspx

If you need to get the CustomAuthorizeAttribute you can use reflection to get information about the class you're in and then pull the properties of the attribute but I would question why you need to. Is there something specific you want that we can help more with?

tbddeveloper
  • 2,407
  • 1
  • 23
  • 39
  • Thank you this was helpful and +1. However the reflection example is for the class and not a method. The one below by George is to the point. – user3885927 Jul 23 '15 at 19:58
2

if you want to get attribute from that method you can do this, for example with reflection:

var atrb = typeof(TestController).GetMethod("Index")
             .GetCustomAttributes(typeof(CustomAuthorizeAttribute), true)
             .FirstOrDefault() as CustomAuthorizeAttribute;

or for the current method;

var atrbCurrentMethod = System.Reflection.MethodBase.GetCurrentMethod()
                .GetCustomAttributes(typeof(CustomAuthorizeAttribute), true)
                .FirstOrDefault() as CustomAuthorizeAttribute;

or a more flexible way, if you want to later create a function, as you stated in your comment:

public CustomAuthorizeAttribute GetCustomAuthorizeAttribute() {
            return new StackTrace().GetFrame(1).GetMethod()
                .GetCustomAttributes(typeof(CustomAuthorizeAttribute), true).FirstOrDefault() as CustomAuthorizeAttribute;
        }
George
  • 777
  • 7
  • 17
  • seems you deleted and posted a new answer. It got changed from using stacktrace to new approach. I used your old code, changed the frame from 0 to 1 and made it a separate method. If I use this new approach how can I go to the calling method instead of current method and how is it better from the old one? – user3885927 Jul 23 '15 at 20:04
  • with this modifications, the old cold is better. but you can look here http://stackoverflow.com/questions/171970/how-can-i-find-the-method-that-called-the-current-method :) – George Jul 23 '15 at 20:06
0

Why not use Roles.GetRolesForUser(); method to get all Roles user is having ? This should have the same results as you would get from Reflection parsing the attributes value.

Abhinav Galodha
  • 9,293
  • 2
  • 31
  • 41