Oleg Sh
Oleg Sh

Reputation: 9013

Get [Authorize] data from controller

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?

Upvotes: 0

Views: 659

Answers (1)

Matt
Matt

Reputation: 1757

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;

Upvotes: 1

Related Questions