c# - How do I get a const string from Web.config? -


for authorization in entity framework application, i've written class check's whether current user in 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 seperated semicolon ;             foreach (string role in roles) {                 if (system.web.security.roles.isuserinrole(role))                     return true;             }             return false;         }      } 

now can allow controller actions carried out users in designated role.

[authorizedesignatedroles(designatedroles = authorizedesignatedroles.delete)] public httpresponsemessage deletethisandthat(long id) { ... } 

the problem not want put name of designated delete group in code in web.config file. doing so, visual studio complains not constant string more.

how can make constant string again?

edit: when leave out const keyword , make delete static readonly instead, compiler says an attribute argument must constant expression, typeof expression or array creation expression of attribute parameter type.

what storing appsettings key in constant , fetch value in isauthorized method:

public class authorizedesignatedroles : authorizeattribute {     public const string delete = "groupauthorizedfordeleteaction";      public string designatedroles { get; set; }      protected override bool isauthorized(system.web.http.controllers.httpactioncontext actioncontext) {     {         // ...          string[] roles = designatedroles.split(';')                                         .select(s => configurationmanager.appsettings[s].tostring())                                         .toarray();          foreach (string role in roles)         {             // ...         }     } } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -