在MVC中添加拦截器实现登录后的权限验证
1.新建一个类 (以下实现了打印日志功能)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcReporistory.Controllers
{
public class LoggerFilter : FilterAttribute, IActionFilter
{ void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext)
{
System.Web.HttpContext.Current.Response.Write("执行action前拦截");
filterContext.Controller.ViewData["ExecutingLogger"] = "正要添加公告,已以写入日志!时间:" + DateTime.Now;
} void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
{
System.Web.HttpContext.Current.Response.Write("执行action后拦截");
filterContext.Controller.ViewData["ExecutedLogger"] = "公告添加完成,已以写入日志!时间:" + DateTime.Now;
}
} }
2.在控制器中用[LoggerFilter] 标识这个控制器类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcReporistory.Controllers
{
[LoggerFilter]
public class BookController : Controller,IActionFilter
{
//
// GET: /Book/ private Domain.IBookRepository.IBookRepository bookRepository;
private Domain.Infrastructure.IUnitOfWork unitOfWork; public BookController()
{
Domain.Infrastructure.BookStoreDbContext bookStoreDbContext = new Domain.Infrastructure.BookStoreDbContext();
bookRepository = new DAL.Repository.BookRepository(bookStoreDbContext);
unitOfWork = new Domain.Infrastructure.UnitOfWork(bookStoreDbContext); } public ActionResult List()
{
IList<Domain.Models.Book> listBook = bookRepository.GetAllBooks(); ViewBag.listdata = listBook;
return View(listBook);
} public ActionResult Cre()
{
Random rn=new Random();
Domain.Models.Book book = new Domain.Models.Book();
book.ISBN = rn.Next(,).ToString();
book.Title = "aaaa";
book.Type = "类型";
Create(book);
return View(); } public ActionResult Create(Domain.Models.Book book)
{
bookRepository.Insert(book);
unitOfWork.Save();
return RedirectToAction("List");
} }
}
在MVC中添加拦截器实现登录后的权限验证的更多相关文章
- spring mvc中的拦截器小结 .
在spring mvc中,拦截器其实比较简单了,下面简单小结并demo下. preHandle:预处理回调方法,实现处理器的预处理(如登录检查),第三个参数为响应的处理器(如我们上一章的Control ...
- Spring MVC中的拦截器/过滤器HandlerInterceptorAdapter的使用
一般情况下,对来自浏览器的请求的拦截,是利用Filter实现的 而在Spring中,基于Filter这种方式可以实现Bean预处理.后处理. 比如注入FilterRegistrationBean,然后 ...
- Spring MVC中的拦截器Interceptor
谈谈spring中的拦截器 在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先设置数据以及统计方法的执行效率等等.今天就来详细的谈一下spring中的拦截器.spring中拦截器主 ...
- SpringMVC拦截器+Spring自定义注解实现权限验证
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- Spring MVC中自定义拦截器的简单示例
1. 引言 拦截器(Interceptor)实现对每一个请求处理前后进行相关的业务处理,类似于Servlet的Filter. 我们可以让普通的Bean实现HandlerIntercpetor接口或继承 ...
- ASP.NET MVC中的拦截器
在ASP.NET MVC中,有三种拦截器:Action拦截器.Result拦截器和Exception拦截器, 所谓的拦截器也没有什么的,只是写一个类,继承另一个类和一个接口,顺便实现接口里面的方法而以 ...
- 9.springMVC中的拦截器
springMVC中的拦截器大概大致可以分为以下几个步骤去学习: 1.自定义一个类实现HandlerInterceptor接口,这里要了解其中几个方法的作用 2.在springMVC的配置文件中添加拦 ...
- 通过配置http拦截器,来进行ajax请求验证用户登录的页面跳转
在.NET中验证用户是否登录或者是否过期,若需要登录时则将请求转向至登录页面. 这个流程在进行页面请求时是没问题的,能正确进行页面跳转. 然而在使用xmlhttprequest时,或者jq的getJs ...
- spring boot中注册拦截器
拦截器是动态拦截Action调用的对象.它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行,同时也提供了一种可以提取action中可重 ...
随机推荐
- 转:jmeter实践
本文主要介绍性能测试中的常用工具jmeter的使用方式,以方便开发人员在自测过程中就能自己动手对系统进行自动压测和模拟用户操作访问请求.最后还用linux下的压测工具ab做了简单对比. 1. ...
- 阅读 LdrInitializeThunk
参考: http://blog.csdn.net/hw_henry2008/article/details/6568255 Windows 的 DLL 装入(除 ntdll.dll 外)和连接是通过 ...
- 改MAC地址
Google TMAC v6. Or click here
- memcached添加IP白名单,只允许指定服务器调用
由于memcached默认安装是不用配置密码的(具体的密码配置我也没怎么研究,据说是有的,大家感兴趣去找一找) 然而memcached链接也是非常简单的 linux命令链接使用 Telnet IP地 ...
- tcp 的6个控制位
原文:http://blog.chinaunix.net/uid-26413668-id-3376762.html TCP(Transmission Control Protocol) 传输控制协议 ...
- 转:sqlplus与shell互相传值的几种情况
sqlplus与shell互相传值的几种情况 情况一:在shell中最简单的调用sqlplus $cat test.sh #!/bin/sh sqlplus oracle/oracle@oracle& ...
- UIScrollView代理方法
手拖拽后会调用 - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView [scrollView setContentOffset ...
- CenOS 用PF_RING优化Snort
0.优化顺序 安装PF_RING的kernel模块 安装PF_RING的用户态库 安装Snort的DAQ 安装PF_RING的pfring-daq-module 安装snort 安装PF_RING-a ...
- iOS LaunchScreen和LaunchImage的转换启动图
今天开始设置一个新项目的启动图,需要我自己设置,我在UI那拿到以前格式的启动图不知道为何需要那么多图,我记得用LaunchScreen只需一张即可,利用自动布局,今天看到这么多图,发现他用的是Laun ...
- Linux服务器软件安装备忘
1.Centos安装Mysql --安装 yum install mysql-server 卸载 yum -e mysql-server --设置为开机启动 chkconfig mysqld on - ...