【发布时间】:2015-11-04 04:20:51
【问题描述】:
有没有办法覆盖AllowAnonymous 属性?我已经实现了从数据库加载用户菜单和按钮的自定义授权,如下所示:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new MyCustomAuthorization()); // Custom Authorization for Rights & Priveleges
}
以上工作正常。
现在,如果用户通过身份验证,我想允许访问某些操作,在这种情况下无需检查授权。示例:
[Authorize]
public class MenusAndButtonsController : BaseController
{
[Authenticated] // my custom attribute that will check if user is logged in or not
public JsonResult GetGeneralMenuAndButtons()
{
using (MealPlannerAuthorizationEntities repository = new MealPlannerAuthorizationEntities())
{
var MenusAndButtons = repository.MP_AUTH_Menus.Where(x => x.IsButton == false && x.IsListButton == false).Select(c => new { DisplayText = c.MenuName, Value = c.MenuId }).OrderBy(x => x.DisplayText).ToList();
return Json(new { Result = "OK", Options = MenusAndButtons }, JsonRequestBehavior.AllowGet);
}
}
}
我正在尝试创建自己的自定义属性[Authenticated] 而不是AllowAnonymous,它将检查用户是否已登录。如果用户已登录,它将返回 true,GetGeneralMenuAndButtons 将继续其操作。
【问题讨论】:
标签: asp.net-mvc-4 authentication custom-attributes filterattribute