Dynamic roles list in CustomAuthorize ASP MVC

141 views Asked by At

In

[CustomAuthorize(Roles="Editor, Admin, Manager")]
  1. How can I use a variable / array of roles in static string Roles="Editor, Admin, Manager" ?
  2. How can I put a string variable or even n list<string> of CSV into the roles?

How to get rolesList into roles for customAttribute

  List<string> rolesList = new List<string>();
    rolesList.add("Editor").add("Admin").add("Manager");
    //...
    // now get that list
    [CustomAuthorize(Roles= `"` + string.Join<string>(",", rolesList) + `"` )]
1

There are 1 answers

1
Erik Funkenbusch On

The short answer is, no. You can't. Attributes are static metadata compiled into your application, and thus cannot be updated at runtime.

The longer answer is that if you really need this functionality, you'll have to implement your own Authorize attribute.