MVC中的授权过滤器

MVC支持的过滤器类型有四种,分别是:Authorization(授权),Action(行为),Result(结果)和Exception(异常)

授权过滤器的使用

新建一个类LoginAuthorityAttribute

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ERPMVC
{
    public class LoginAuthorityAttribute:AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext.Session["Id"] == null)
            {
                filterContext.HttpContext.Response.Redirect("/UI/Login");
            }
        }
    }
}

在需要使用过滤器的控制器namespace下面添加[LoginAuthority]

猜你喜欢

转载自www.cnblogs.com/dujian123/p/10647872.html
今日推荐