0

I have code:

[Authorize(Roles = "SuperAdministrators")]
public class ButtonStyleController : ControllerBase
{

in other place:

[Authorize(Roles = "SuperAdministrators,CompanyAdministrators")]
public class BankController : ControllerBase
{

and even:

[Authorize(Roles = "CompanyAdministrators")]
public class DriverController : ApiControllerBase
{

I need to check which roles are allowed for current controller in code. Is it possible?

Oleg Sh
  • 8,496
  • 17
  • 89
  • 159
  • 1
    You do it the same way you read any attribute on a class. See here: [How do I read an attribute on a class at runtime?](https://stackoverflow.com/questions/2656189/how-do-i-read-an-attribute-on-a-class-at-runtime) – Gabriel Luci Aug 10 '19 at 20:18

1 Answers1

1

You can use the following code to get an attribute, of type AuthorizeAttribute, from a class then access the Roles property.

AuthorizeAttribute currentAuthorizeAttribute = (AuthorizeAttribute)Attribute.GetCustomAttribute(typeof(DriverController), typeof(AuthorizeAttribute));
string roles = currentAuthorizeAttribute.Roles;
Matt
  • 1,749
  • 2
  • 12
  • 26