7

I just want don't use "Managers" for each attribute and use some enum for that.

But it seems it is impossible or I am wrong?

So I try to replace

[RequiresRole("Managers")]

with

[RequiresRole(HardCodedRoles.Managers.ToString())]

...

public enum HardCodedRoles
{ 
            Administrators,
            Managers
}
NoWar
  • 36,338
  • 80
  • 323
  • 498
  • 1
    Could you add more detail so that we know what it is you are trying to accomplish? – Maciej Aug 29 '12 at 16:41
  • I just don't want to use string "AnyRoleName" and I want to put some item from enum instead of it. But VS gives an error An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type – NoWar Aug 29 '12 at 16:42
  • 1
    if HardcodedRoles.Managers is a const string, this would work. On the other hand if HardcodedRoles is an enumeration, you will have to subclass the AuthorizeAttribute and implement your own, passing HardcodedRoles and/or HardcodedRoles[] to the constructor. – Maciej Aug 29 '12 at 16:48
  • @Maciej Could u provide any sample of it please? I just found some link http://stackoverflow.com/questions/2397923/asp-net-mvc-dynamic-authorization and here http://stackoverflow.com/questions/1315524/is-it-possible-to-override-the-default-behavior-of-authorize-in-asp-net-mvc and here http://geekswithblogs.net/thomasthedeuce/archive/2009/06/25/133056.aspx but cannot figure out how it might be done. – NoWar Aug 29 '12 at 16:55

3 Answers3

11

How about a class instead of an enum, making the class static to avoid somebody new:ing it ?

public static class HardCodedRoles
{
    public const string Managers = "Managers";
    public const string Administrators = "Administrators";
}

[RequiresRole(HardCodedRoles.Managers)] 
Tommy Grovnes
  • 4,126
  • 2
  • 25
  • 40
7

You could also use the nameof keyword, i.e.:

[RequiresRole(nameof(HardCodedRoles.Managers))]
dns_nx
  • 3,651
  • 4
  • 37
  • 66
  • Why isn't this the accepted answer? The question is how to use an enum as an attribute, and this is correct. The accepted answer doesn't even use enums. – Tony Cheetham May 09 '18 at 20:07
2

The reason you see the error is because ToString() is a method and thus the value cannot be calculated at compile time.

If you can use [RequiresRole(HardCodedRoles.Managers)] instead, you can perform the ToString elsewhere in your code, and this could give you the functionality you need. This will require that you change the parameter of your attribute from string to HardCodedRoles.

(I would imagine that using a const won't work, because the type of the parameter will still be string, so the input won't be restricted.)

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 1
    Thank you! Could u please provide any sample of "If you can use [RequiresRole(HardCodedRoles.Managers)] instead, you can perform the ToString elsewhere in your code, and this could give you the functionality you need. " – NoWar Aug 29 '12 at 17:01
  • What do you need a sample of? – Dan Puzey Aug 29 '12 at 18:56