Authorize attribute using constant expression

I am trying to install Authorize for my controllers, can do the following:

[Authorize(Roles = "Approver")]

Be that as it may, role names are stored in the database, so I would like to try to do the following:

[Authorize(Roles = Settings.Instance.RoleEmployee)]

but I get the following error:

The attribute argument must be a constant expression, typeof expression, or an array creation expression type attribute attribute

How to get around this?

+3
source share
3 answers
public class UniqueAttribute : ValidationAttribute
{        
    public string Identifier { get; set; }

    public override bool IsValid(object value)
    {          
        // Get property value
        PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
        string identifierValue = properties.Find(this.Identifier,     true).GetValue(value).ToString();
    }
}

You will get the value of any property like above

   [UniqueAttribute(Identifier = "Id")]
0
source

If Employee is a known role, define this string constant in your application and make sure that the role stored in the database can be matched to this value when required.

0
source

Source: https://habr.com/ru/post/1713840/


All Articles