在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中可重 ...
随机推荐
- 修改6S Fortran77 代码,建立查找表
逐像元大气校正,常预先计算查找表(LUT,LookUp Tabel),6S大气辐射传输模式也可以用来计算LUT.但6S源程序输出信息多,且浮点数输出精度低,不利于提取关键信息生成LUT,本文描述了 ...
- 转:Warning -26490: File name in a multipart submit is missing or empty.解决方法
录制测试上传文件脚本,回放报Warning -26490: File name in a multipart submit is missing or empty. Using an empty fi ...
- PAT (Advanced Level) 1108. Finding Average (20)
简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
- hdu_4467_Graph(莫队算法思想)
题目连接:hdu_4467_Graph 题意:给你n个点,m条边,每条边有一个权值,有两个操作,一个是修改单点的颜色,一个是询问边的两个端点都为指定颜色的权值和 题解:这题如果暴力的话,就是维护3个a ...
- jq的事件对象的属性
1.event.type() 该方法的作用是可以获取到时间的类型 $('a').click(function(){ alert(event.type);//获取事件类型 return false;/ ...
- 2016青岛网络赛 Barricade
Barricade Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Proble ...
- initWithNibName与viewDidLoad的执行关系以及顺序
转载自:http://www.java123.net/v/380331.html 一个ViewController,一般通过init或initWithNibName来加载.二者没有什么不同,init最 ...
- (转)Permission denied: win7下面eclipse上传本地文件到DFS && 运行M/R程序时出现的同样的错误解决方法
原文地址: http://mntms.iteye.com/blog/2095651 hadoopeclipse远程控制权限 情景一: 当在win7下面的eclipse装好插件,首次运行M/R程序的时 ...
- Activity中 左滑动返回监听
网易新闻中有个比较炫的效果,在QQ进入聊天界面也有这种效果,就是从界面左侧滑动到右侧时,界面退出,其实功能很容易实现: 1) Activity 去实现 implements OnTouchListen ...
- Qt5:QSystemTrayIcon类实现程序托盘图标
windows下,在许多应用程序中都会实现一个托盘图标,用于隐藏应用程序窗口时还能对该应用程序进行简单的操作,例如 QQ ,renren等程序 那么,在Qt中,如何实现呢? 这就要用到Qt提供的 QS ...