.Net Core MVC全局过滤器验证是否需要登录
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全局过滤器验证是否需要登录的更多相关文章
- asp.net mvc 自定义全局过滤器 验证用户是否登录
一般具有用户模块的系统都需要对用户是否登录进行验证,如果用户登录了就可以继续操作,否则退回用户的登录页面 对于这样的需求我们可以通过自定义一个独立的方法来完成验证的操作,但是这样代码的重复率就大大提高 ...
- 一、Core基于MVC的全局过滤器验证
一.Core基于MVC的过滤器验证 1.添加一个过滤器.在Startup 中ConfigureServices方法里添加一个Filters 即我们自己授权代码类. public void Config ...
- asp.net core MVC 全局过滤器之ExceptionFilter异常过滤器(一)
本系类将会讲解asp.net core MVC中的内置全局过滤器的使用,将分为以下章节 asp.net core MVC 过滤器之ExceptionFilter异常过滤器(一) asp.net cor ...
- MVC 自定义过滤器/特性来实现登录授权及验证
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 最近在做自学MVC,遇到的问题很多,索性一点点总结 ...
- .net core MVC Filters 过滤器介绍
一.过滤器的优级依次介绍如下(逐次递减): Authorization Filter -> Resource Filter -> Acton Filter -> Exception ...
- ASP.NET Core MVC 之过滤器(Filter)
ASP.NET MVC 中的过滤器允许在执行管道中的特定阶段之前或之后运行代码.可以对全局,也可以对每个控制器或每个操作配置过滤器. 1.过滤器如何工作 不同的过滤器类型在管道中的不同阶段执行,因此具 ...
- .Net Core MVC中过滤器简介
在.Net Framework MVC 中有四种过滤器,授权过滤器(Authorize).Action 过滤器.结果过滤器(Result).异常过滤器(Exception)四种过滤器.在.Net Co ...
- Mvc全局过滤器与Action排除
http://blog.csdn.net/shuaihj/article/details/53020428 如何一次性给所有action做登录验证过滤,如何排除不需要做登录验证的action? 1. ...
- mvc全局过滤器和httpmodule的执行顺序
根据http管线模型,请求先通过httpmodule,再通过httphandler,之后再进入mvc的过滤器 另外参考:MVC如何在Pipeline中接管请求的? http://www.cnblogs ...
随机推荐
- java meil
import java.util.Date; import java.util.List; import java.util.Properties; import javax.activation.D ...
- ABP Framework 5.0 RC.1 新特性和变更说明
.Net 6.0 发布之后,ABP Framework 也在第一时间进行了升级,并在一个多星期后(2021-11-16)发布了 5.0 RC.1 ,新功能和重要变更基本已经确定. 5.0版本新特性 新 ...
- [loj2863]组合动作
先用两次猜出第一个字符,后面就不会出现这个字符了 (我们假设这个字符是c0,其余三种字符分别是c1.c2和c3) ,然后考虑已知s的前i个字符(不妨就s),来推出后面的字符 询问:s+c1和s+c2, ...
- [bzoj1190]梦幻岛宝珠
根据$2^b$分组,组内处理出g[i][j]表示当容量为$j\cdot 2^{i}$且只能选b=i时最大价值,再组间dp用f[i][j]表示当容量为$j\cdot 2^{i}+(w\&(2^{ ...
- 描述高频题之队列&栈
栈和队列 全文概览 基础知识 栈 栈是一种先进后出的数据结构.这里有一个非常典型的例子,就是堆叠盘子.我们在放盘子的时候,只能从下往上一个一个的放:在取的时候,只能从上往下一个一个取,不能从中间随意取 ...
- Codeforces 575A - Fibonotci
题面传送门 题意: 给出 \(s_0,s_1,s_2,\dots,s_{n-1}\),对于 \(i\geq n\),有 \(m\) 个 \(s_i\) 满足 \(s_i\neq s_{i\bmod n ...
- 【豆科基因组】木豆Pigeonpea (Cajanus cajan) 292个自然群体重测序2017NG
目录 一.来源 二.结果 一.来源 Whole-genome resequencing of 292 pigeonpea accessions identifies genomic regions a ...
- 【Perl】如何安装Bioperl模块?
目录 失败尝试一:使用cpanm 失败尝试二:使用CPAN 成功尝试:直接conda安装bioperl 没有尝试:源码安装bioperl 生信软件绕不过Perl,Perl绕不过Bioperl.而Bio ...
- 全基因组选择育种(GS)简介
全基因组选择(Genomic selection, GS)是一种利用覆盖全基因组的高密度标记进行选择育种的新方法,可通过早期选择缩短世代间隔,提高育种值(Genomic Estimated Breed ...
- BaiduPCS-Go----百度云下载工具
1.网页登录百度网盘:https://pan.baidu.com/2.百度输入法生成:http://pcs.baidu.com/rest/2.0/pcs/file?app_id=265486& ...