1

我需要 [Authorize] 像 ASP.Net mvc 3/4 中的自定义属性。像下面的东西。

[AdminOnly]
public ActionResult OpenAddListUser()
{
    //Do some actions
}

这里 [AdminOnly] 将检查一些用户凭证。我所需要的只是如果 AdminOnly 无效,则返回一些 ActionResult 视图或重定向到其他一些视图,如登录。

4

1 回答 1

3
  public class AdminOnly : AuthorizeAttribute
    {

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            bool baseAuthorized = base.AuthorizeCore(httpContext);
            if (!baseAuthorized) {
                     return false;
            }
            //here should be your admin checking logic
            bool isAdmin = YourLogic.IsAdmin(httpContext.User.Identity.Name);
            return isAdmin;
        }

    }
}
于 2013-07-02T11:06:04.790 回答