For authorization in an Entity Framework application, I've written this class which check's whether the current user is in the specified role.
public class AuthorizeDesignatedRoles : AuthorizeAttribute {
public const string DELETE = System.Configuration.ConfigurationManager.AppSettings["GroupAuthorizedForDeleteAction"].ToString();
public string DesignatedRoles { get; set; }
protected override bool IsAuthorized(System.Web.Http.Controllers.HttpActionContext actionContext) {
bool isAuthorizedBase = base.IsAuthorized(actionContext);
if (!isAuthorizedBase)
return false;
string[] roles = DesignatedRoles.Split(';'); // Multiple roles can be seperated by a semicolon ;
foreach (string role in roles) {
if (System.Web.Security.Roles.IsUserInRole(role))
return true;
}
return false;
}
}
Now I can allow controller actions only to be carried out by users who are in a designated role.
[AuthorizeDesignatedRoles(DesignatedRoles = AuthorizeDesignatedRoles.DELETE)]
public HttpResponseMessage DeleteThisAndThat(long id) { ... }
The problem is that I do not want to put the name of the designated DELETE group in the code but in the web.config file. By doing so, Visual Studio complains that it is not a constant string any more.
How can I make it a constant string again?
Edit: When I leave out the const
keyword and make DELETE static readonly
instead, the compiler says An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.