创建一个AuthenticateFilterAttribute(即过滤器/拦截器)

引用System.Web.Mvc;

public class AuthenticateFilterAttribute: ActionFilterAttribute,IActionFilter
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // Action或Controller上定义了[AllowAnonymousAttribute]
            var hasAllowAnymous = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
                                  filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true);

            if (hasAllowAnymous) return; // 不需要登录,则通过。

            var session = filterContext.HttpContext.Session;

            if (session == null) return;

            //下面就是写过滤的条件,一般来说就是判断session是否过期,跳转到登陆页面。
            //以及判断角色,身份




        }
    }        
AuthenticateFilterAttribute.cs

相关文章:

  • 2021-06-09
  • 2021-09-14
  • 2021-12-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-25
猜你喜欢
  • 2021-07-11
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-04-10
  • 2021-11-30
  • 2021-11-30
相关资源
相似解决方案