I have an issue regarding a custom authorization attribule (MVC 5) and a list of enumerations which represent the user's role. First i have this custom authorization attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public sealed class CustomAuthorizationAttribute : AuthorizeAttribute
{
public Enums.Roles[] AllowedRoles { get; set; }
}
A role enumeration
public enum Roles
{
uknown = 0,
Admin= 100,
Guest = 200,
}
A static list which I use to restrict user access to certain controllers and methods which i want to use
public static class AuthorizationHelpers
{
public static readonly Enums.Roles[] AccessToIndivindual = {
Enums.Roles.Admin,
Enums.Roles.Guest,
};
}
In my controller when I use the following and specify the list of enumeration roles, the authorization works as expected
[CustomAuthorization(AllowedRoles = new[] { Enums.Roles.Admin, Enums.Roles.Guest})]
public class HomeController
{
....
}
What i need is to use the static readonly enumeration list with all the roles I want to allow to access the controller/method like AccessToIndivindual. in order to reuse them. I tried something like this
[CustomAuthorization(AllowedRoles = AuthorizationHelpers.AccessToIndivindual )]
public class HomeController
{
....
}
but every time I use it like this i get
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
Is there a way i can use a list of roles as enumerations as an attribute parameter to achieve this?