1.新建MVC项目

2.找到Models文件夹,新建 LoginCheckFilterAttribute 类

  public class LoginCheckFilterAttribute : ActionFilterAttribute
{
public bool IsCheck { get; set; } public override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext); if (IsCheck)
{
//校验用户是否已经登录
if (filterContext.HttpContext.Session["loginUser"] == null)
{
filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
}
} filterContext.HttpContext.Response.Write("开始时间:" + DateTime.Now.ToString() + "<br/>");
} public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);
var controllerName = filterContext.RouteData.Values["controller"].ToString();
var actionName = filterContext.RouteData.Values["action"].ToString();
filterContext.HttpContext.Response.Write("结束时间:" + DateTime.Now.ToString() + "<br/>");
filterContext.HttpContext.Response.Write("controller:" + controllerName + ",action:" + actionName);
}
}

LoginCheckFilterAttribute

3.找到App_Start下的 FilterConfig.cs 添加以下代码

     public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute()); filters.Add(new LoginCheckFilterAttribute() { IsCheck = true });
}
}

FilterConfig

4.使用方式

 [LoginCheckFilter(IsCheck = false)]
public class UserLoginController : BaseController
{
public UserLoginController()
{
this.IsCheckLogin = false;
}
//
// GET: /UserLogin/ [LoginCheckFilter(IsCheck = true)]
public ActionResult Index()
{
return View();
}
}

UserLoginController

End

1.扩展基类进行权限控制

  public class BaseController : Controller
{
//在当前控制器所有方法之前执行,都先执行此代码
public bool IsCheckLogin = true; protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext); if (IsCheckLogin)
{
//校验用户是否已经登录
if (filterContext.HttpContext.Session["loginUser"] == null)
{
filterContext.HttpContext.Response.Redirect("/UserLogin/Index");
}
}
}
}

BaseController

2.调用方式

  public class UserLoginController : BaseController
{
public UserLoginController()
{
this.IsCheckLogin = false;
}
}

UserLoginController

ASP.NET MVC使用过滤器进行权限控制的更多相关文章

  1. ASP.NET MVC 和 WebForm的权限控制

    今天主要讲一下对于ASP.NET的页面级权限控制 数据结构:用户表.角色表.权限表.角色权限派生表 为用户添加权限的数据配置后, 自定义类对MVC继承Controller 对其内置方法Initiali ...

  2. ASP.NET MVC 基于角色的权限控制系统的示例教程

    上一次在 .NET MVC 用户权限管理示例教程中讲解了ASP.NET MVC 通过AuthorizeAttribute类的OnAuthorization方法讲解了粗粒度控制权限的方法,接下来讲解基于 ...

  3. 使用ASP.NET MVC操作过滤器记录日志(转)

    使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_Filte ...

  4. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  5. [翻译] 使用ASP.NET MVC操作过滤器记录日志

    [翻译] 使用ASP.NET MVC操作过滤器记录日志 原文地址:http://www.singingeels.com/Articles/Logging_with_ASPNET_MVC_Action_ ...

  6. ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目

    ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml)   我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为 ...

  7. FineAdmin.Mvc 使用ok-admin+ASP.NET MVC搭建的通用权限后台管理系统

    FineAdmin.Mvc 介绍 使用ok-admin+ASP.NET MVC搭建的通用权限后台管理系统RightControl后台layui模板不太好看,换成ok-admin v2.0重写一遍.项目 ...

  8. ASP.NET MVC 系统过滤器、自定义过滤器

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  9. ASP.NET MVC动作过滤器

    ASP.NET MVC提供了4种不同的动作过滤器(Aciton Filter). 1.Authorization Filter 在执行任何Filter或Action之前被执行,用于身份验证 2.Act ...

随机推荐

  1. iOS CLLocationManager 定位

    今天写个定位,本来很简单,但是在填写plist时,通过系统提示,只能看到NSLocationUsageDescription项目,根本不提示 (1)NSLocationAlwaysUsageDescr ...

  2. ios 在中国地区,24小时时间格式 系统设定下 获得12小时制时间的方法

    如题,在中国地区,24小时时间格式 系统设定下,如果单单使用 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 之后,无论用hh ...

  3. Android 程序中得到root activity的引用

    今天写anroid时,想获得一个root activity的引用. ios中这个很简单, [UIApplication sharedApplication].keyWindow 得到window对象, ...

  4. lftp使用普通ftp模式登录

    set ftp:use-feat no set ftp:passive-mode yes set ftp:ssl-protect-data no set ssl:verify-certificate ...

  5. ACM/ICPC 之 平面几何-两直线关系(POJ 1269)

    题意:给定四点的坐标(x,y),分别确定两直线,求出其交点,若重合or平行则输出相应信息 用四个点的坐标算出直线通式(ax+by+c=0)中的a,b,c,然后利用a,b,c计算出交点坐标(其他公式不够 ...

  6. Python: 程序print到文件中

    Python 3.x 将输出内容写入到一个文件,需要两个命令.open指明Python用什么文件名,w 意味着我们要写入该文件, encoding=”utf-8″指明Python如何把中文写入该文件. ...

  7. 汉企PHP开班

    明天PHP正式开班,没什么大目标 ,在四个半月的时间吧基础知识掌握牢固,自信的面对企业.

  8. 自定义循环滑动的viewpager

    今天和大家分享一下如何定制一个可以循环滑动的viewpager.其实今天更重要的提供一种组件化思想,当然你可以理解为面向对象思想. 吐槽一下网上流行的实现方式吧(为了方便说明,下文称之为方式A),方式 ...

  9. IOS - 内购

    内购的五种产品类别 •非消耗品(Nonconsumable)买了就有,头衔,功能 –指的是在游戏中一次性购买并拥有永久访问权的物品或服务.非消耗品物品可以被用户再次下载,并且能够在用户的所有设备上使用 ...

  10. 执行HQL语句出现Remember that ordinal parameters are 1-based

    今天使用hibernate出个奇怪的错误,第一次碰到 Remember that ordinal parameters are 1-based! 原因是 问题发生的原因是:hql语句里不需要参数,却添 ...