你的位置:首页 > ASP.net教程

[ASP.net教程]mvc登录授权特性


  public class CommonAuthorize : AuthorizeAttribute  {    protected override bool AuthorizeCore(HttpContextBase httpContext)    {      return UserHelper.CurrentUser != null;    }    public override void OnAuthorization(AuthorizationContext filterContext)    {      if (!filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) && !filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true))      {        if (UserHelper.CurrentUser == null)        {          filterContext.Result = new RedirectResult(string.Format("/OAuth2/IndexUserInfo?returnUrl={0}", filterContext.HttpContext.Request.RawUrl));        }      }    }  }

 使用

[CommonAuthorize]  public class BaseController : Controller  {}