I have these two line:
[AccessDeniedAuthorize(new string[] { "MIS", "Test" })]
[AccessDeniedAuthorize(DAL.RoleHandler.GetRolesForTcode("testMenu"))]
But second line has this error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
But GetRolesForTcode
returns string[]
too, why the error?
public class AccessDeniedAuthorizeAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
base.OnAuthorization(filterContext);
if (filterContext.Result is HttpUnauthorizedResult)
{
filterContext.Result = new RedirectToRouteResult(new System.Web.Routing.RouteValueDictionary { { "Controller", "AccessDenied" }, { "Action", "Index" } });
}
}
public AccessDeniedAuthorizeAttribute(string[] roles)
{
this.Roles = string.Join(",", roles);
}
}
public class RoleHandler
{
public static string[] GetRolesForTcode(string tCode)
{
using (var db = new Models.VMIEntities())
{
var roles = db.T_Auth_Role_Master
.Where(role => string.Compare(role.obj, "t_code", StringComparison.OrdinalIgnoreCase) == 0)
.Where(role => string.Compare(role.authobj_value, tCode, StringComparison.OrdinalIgnoreCase) == 0)
.Select(role => role.role);
return roles.ToArray();
}
}
}