码迷,mamicode.com
首页 > Web开发 > 详细

MVC中的授权过滤器

时间:2019-04-03 12:48:16      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:idt   ssi   一个   strong   类型   red   contex   session   log   

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]

技术图片

MVC中的授权过滤器

标签:idt   ssi   一个   strong   类型   red   contex   session   log   

原文地址:https://www.cnblogs.com/dujian123/p/10647872.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!