0

Is there any way to check schema annotation from a base class? Is simple to explain throw an example:

I have this 2 controllers

[Authorize]
public class HomeController : _baseController
{
   //Some actions here    
}

[AllowAnonymous]
public class OtherController : _baseController
{
   //Some actions here    
}

Then I have this base class which overrides OnActionExecuting. The objective is to perform some action if the controller has an annotation.

public class _baseController
{
   public override void OnActionExecuting(ActionExecutingContext context)
    {
        base.OnActionExecuting(context);

        if(context.Controller.hasAnnotation("Authorize")){
              //do something
        }
        else if(context.Controller.hasAnnotation("AllowAnonymous")){
              //do something
        }
    }    
}

Obviously context.Controller.hasAnnotation is not a valid method. But you get the idea.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Rumpelstinsk
  • 3,107
  • 3
  • 30
  • 57
  • 1
    Have you tried something like in this post? https://stackoverflow.com/questions/1226161/test-if-a-class-has-an-attribute So something like `Attribute.GetCustomAttribute(typeof(context.Controller), typeof(AuthorizeAttribute))` You'll also need `using System.Reflection` to use the GetCustomAttribute extension. That method will return the attribute, or null if it doesn't exist on that class – Sam Walpole Oct 25 '19 at 10:10
  • @SamWalpole Perfect, thats exactly what i was looking for. I will accept it, if you post it like an answer. Thanks for the help – Rumpelstinsk Oct 25 '19 at 10:20

2 Answers2

2

Further to my comment above, I have tested the following solution in ASP.Net Core 3.

public override void OnActionExecuting(ActionExecutingContext context)
{
  var allowAnonAttr = Attribute.GetCustomAttribute(context.Controller.GetType(), typeof(AllowAnonymousAttribute));

  if(allowAnonAttr != null)
  {
    // do something
  }
}

In older versions of ASP.NET, you also have to reference System.Reflection to make use of the GetCustomAttribute extension.

Note that this solution works for attributes placed on the controller class itself (as asked in the question), but will not work for attributes placed on action methods. To get it to work for action methods, the below works:

public override void OnActionExecuting(ActionExecutingContext context)
{
  var descriptor = context.ActionDescriptor as ControllerActionDescriptor;
  var actionName = descriptor.ActionName;
  var actionType = context.Controller.GetType().GetMethod(actionName);

  var allowAnonAttr = Attribute.GetCustomAttribute(actionType, typeof(AllowAnonymousAttribute));

  if(allowAnonAttr != null)
  {
    // do something
  }
}
Sam Walpole
  • 1,116
  • 11
  • 26
0

As suggested in the comments, the below should work for you:

public class _baseController
{
public override void OnActionExecuting(ActionExecutingContext context)
{
    base.OnActionExecuting(context);

   System.Attribute[] attrs = System.Attribute.GetCustomAttributes(context.Controller.GetType());
}    
}
dori naji
  • 980
  • 1
  • 16
  • 41