Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put an array of enum in a authorize attribute?

I'm trying to do something like this:

   [CustomAuthorize(Permissions = new[] { /*These are Enums */Permissions.CanChangeProducts.ToString(), Permissions.CanChangeNames.ToString()})]
    public ActionResult MyMethod()
    {
        return View();
    }

It gives the error:

Error 5 An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type [...]

I've managed to put Resource string into validationattibutes, but I could'n manage to put this enum (string) array into a authorize attribute... How can I do that? Any help would be appreciated!

like image 862
Victor Franchi Avatar asked Dec 16 '25 17:12

Victor Franchi


1 Answers

enum.Value | enum.Value | enum.Value

What about flags?

Like this:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Enum)]

Edit: By the way, there is very interesting answer: https://stackoverflow.com/a/270231/2524304

like image 117
Maxim Zhukov Avatar answered Dec 19 '25 11:12

Maxim Zhukov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!