title: How can I use a variable in [Authorize(Roles="")]
body: <p>I have an MVC 5 C# intranet web application where we have over 30 Active Directory roles in use, and permissions are often a changing thing due to the business culture.</p>
  
  <p>To make things easy for myself, I thought I would try something like this to determine who is allowed access to a controller action or child action.</p>
  
  <pre><code>/* This function runs a LINQ query and outputs a comma delimited string of 
     approved active directory roles.
  */
  
      private static string _approvedRoles = 
              Helpers.QueryableExtensions.GetApprovedRoles("FourCourseAudit");
  
      // GET: FourCourseAudits    
      [Authorize(Roles = _approvedRoles)]
      public ActionResult Index(string searchBy="All", 
            string orderBy="Campus", string orderDir="Asc")
      {
  
      // and so on... 
  </code></pre>
  
  <p>Unfortunately, I get this compile time error: <code>An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type</code>.</p>
  
  <p>This is where I am after trying other approaches with the <code>_approvedRoles</code> variable, such as <code>public const string</code> and <code>public string</code>. I placed the <code>GetApprovedRoles</code> function in the model, in the repository (where it is now), and in the body of the controller.  </p>
  
  <p>I know the roles are good because if I use this: <code>[Authorize(Roles="DOMAIN\Role1,DOMAIN\Role2")]</code> it works. And that's not a feasible option for me because the roles change and this is a very large MVC site. Is there some way I can let Roles be a variable?</p>