1.新增全局登录过滤器LoginCheckAttribute

 1 public class LoginCheckAttribute: ActionFilterAttribute
2 {
3   public override void OnActionExecuting(ActionExecutingContext filterContext)
4   {
5   // 判断是否检查登陆
6   var noNeedCheck = false;
7   string msg = filterContext.HttpContext.Request.Path;
8
9   if (filterContext.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor)
10   {
11     noNeedCheck = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
12 .Any(a => a.GetType().Equals(typeof(NoNeedLoginAttribute)));
13   }
14   if (noNeedCheck) return;
15
16   bool isLogin = filterContext.HttpContext.Session.GetString("_Name") !=null?true:false;//你的验证是否登录方法
17   if (!isLogin)
18   {
19     filterContext.HttpContext.Response.Redirect("/Account/Login", true);//跳转登录页面
20   }
21
22   base.OnActionExecuting(filterContext);
23 }
24 }

2.新增无需登录过滤器NoNeedLoginAttribute

1 public class NoNeedLoginAttribute: ActionFilterAttribute
2 {
3   public override void OnActionExecuting(ActionExecutingContext filterContext)
4 {
5     base.OnActionExecuting(filterContext);
6
7   }
8 }

3.Startup.cs页面添加配置

1 public void ConfigureServices(IServiceCollection services)
2 {
3   //全局过滤器
4 services.AddMvc(config => config.Filters.Add(new LoginCheckAttribute()));
5 }

4.控制器中使用

1 public class AccountController : Controller
2 {
3 [NoNeedLogin]
4 public IActionResult Login()
5 {
6 return View();
7 }
8 }

  

PS:刚开始学习.net Core 有不对的地方,请大家帮忙指正。另外有没有直接在控制器上过滤的,有知道的朋友请在评论中帮忙解答下,谢谢!

例:

 1 public class LoginCheckAttribute: ActionFilterAttribute
2 {
3 public bool IsChecked { get; set; }
4 public override void OnActionExecuting(ActionExecutingContext filterContext)
5 {
6
7 if (IsChecked)
8 {
9 bool isLogin = filterContext.HpptContext.Session.GetString("_Name")!=null? true:false;//你的验证是否登录方法
10 if (!isLogin)
11 {
12 filterContext.HttpContext.Response.Redirect("/Account/Login", false);
13 }
14 }
15
16 base.OnActionExecuting(filterContext);
17 }
18 }
19
20
21 //Startup.cs页面添加配置
22 public void ConfigureServices(IServiceCollection services)
23 {
24   //全局过滤器
25 services.AddMvc(config => config.Filters.Add(new LoginCheckAttribute(){IsChecked = true}));
26 }
27
28
29 //控制器
30 //1.该控制器不需要验证登录
31 [LoginCheck(IsChecked = false)]
32 public class AccountController : Controller
33 {
34 public IActionResult Login()
35 {
36 return View();
37 }
38 }
39
40
41 //2.该控制器需要验证是否登录
42 public class UserController : Controller
43 {
44 public IActionResult Index()
45 {
46 return View();
47 }
48 }

  

.Net Core MVC全局过滤器验证是否需要登录的更多相关文章

  1. asp.net mvc 自定义全局过滤器 验证用户是否登录

    一般具有用户模块的系统都需要对用户是否登录进行验证,如果用户登录了就可以继续操作,否则退回用户的登录页面 对于这样的需求我们可以通过自定义一个独立的方法来完成验证的操作,但是这样代码的重复率就大大提高 ...

  2. 一、Core基于MVC的全局过滤器验证

    一.Core基于MVC的过滤器验证 1.添加一个过滤器.在Startup 中ConfigureServices方法里添加一个Filters 即我们自己授权代码类. public void Config ...

  3. asp.net core MVC 全局过滤器之ExceptionFilter异常过滤器(一)

    本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter异常过滤器(一) asp.net cor ...

  4. MVC 自定义过滤器/特性来实现登录授权及验证

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    最近在做自学MVC,遇到的问题很多,索性一点点总结 ...

  5. .net core MVC Filters 过滤器介绍

    一.过滤器的优级依次介绍如下(逐次递减): Authorization Filter ->  Resource Filter -> Acton Filter -> Exception ...

  6. ASP.NET Core MVC 之过滤器(Filter)

    ASP.NET MVC 中的过滤器允许在执行管道中的特定阶段之前或之后运行代码.可以对全局,也可以对每个控制器或每个操作配置过滤器. 1.过滤器如何工作 不同的过滤器类型在管道中的不同阶段执行,因此具 ...

  7. .Net Core MVC中过滤器简介

    在.Net Framework MVC 中有四种过滤器,授权过滤器(Authorize).Action 过滤器.结果过滤器(Result).异常过滤器(Exception)四种过滤器.在.Net Co ...

  8. Mvc全局过滤器与Action排除

    http://blog.csdn.net/shuaihj/article/details/53020428 如何一次性给所有action做登录验证过滤,如何排除不需要做登录验证的action? 1. ...

  9. mvc全局过滤器和httpmodule的执行顺序

    根据http管线模型,请求先通过httpmodule,再通过httphandler,之后再进入mvc的过滤器 另外参考:MVC如何在Pipeline中接管请求的? http://www.cnblogs ...

随机推荐

  1. MyBatis 中为什么不建议使用 where 1=1?

    最近接手了一个老项目,"愉悦的心情"自然无以言表,做开发的朋友都懂,这里就不多说了,都是泪... ​ 接手老项目,自然是要先熟悉一下业务代码,然而在翻阅 mapper 文件时,发现 ...

  2. ES6基础知识(Generator 函数)

    1.next().throw().return() 的共同点 next().throw().return()这三个方法本质上是同一件事,可以放在一起理解.它们的作用都是让 Generator 函数恢复 ...

  3. java 获得 微信 UserId

    .... public String cs() throws Exception{ /*访问页面,服务器会得到 code(request.getParameter("code")) ...

  4. 教你用SQL进行数据分析

    摘要:采用 SQL 作为数据查询和分析的入口是一种数据全栈的思路. 本文分享自华为云社区<如何使用 SQL 对数据进行分析?>,作者:zuozewei . 前言 我们通过 OLTP(联机事 ...

  5. [hdu6588]Function

    令$m=\lfloor \sqrt[3]{n} \rfloor-1$     $\sum_{i=1}^{n}gcd(floor(\sqrt[3]{i}),i)$=$\sum_{i=1}^{m}\sum ...

  6. [luogu4718]Pollard-Rho算法

    模板题 题解主要分为两部分,即Miller-Robin判素数以及关于Pollard-Rho算法 1.Miller-Robin判素数 对于一个数$n$,判定其是否为素数,依次执行以下几步-- (1)若$ ...

  7. 青龙+Nvjdc短信登陆对接Xdd-plus推送+Ninja CK登陆教程(11.23更新)

    一.准备工作 1.shh工具(powshell.gitbash等等) 2.购买一台云服务器(阿里云.腾讯云都可以) 3.安装宝塔面板 宝塔Linux面板安装教程 - 2021年8月18日更新 - 7. ...

  8. 力扣 - 剑指 Offer 27. 二叉树的镜像

    题目 剑指 Offer 27. 二叉树的镜像 思路1(递归) 我们可以使用深度优先搜索,先递归到链表的末尾,然后从末尾开始两两交换.就相当于后续遍历而已 记得要先保存下来node.right节点,因为 ...

  9. 权限树的制作(menu)

    原来demo 实体类:get.set 1.使用递归的方式将数据查询出来. 2.为了减少数据库交互,一次查询所有数据,遍历集合,然后先判断父节点,如果不是父节点,用原集合重新遍历 3.map形式减少遍历 ...

  10. vue简单语法梳理

    小图不够清楚,可以点击大图查看.