找回密码
 会员注册
查看: 148|回复: 0

Asp.net Core 过滤器(AuthorizationFilter,ActionFilter)

[复制链接]

1389

主题

5

回帖

496万

积分

管理员

积分
4962990
发表于 2024-2-29 08:07:10 | 显示全部楼层 |阅读模式

asp.net core 里面的过滤器有这么多(还提供的异步的过滤器):

IActionFilter

IResourceFilter

IResultFilter

IAuthorizationFilter

IPageFilter

IExceptionFilter

一、身份验证过滤器AuthorizationFilter

  1. using Microsoft.AspNetCore.Mvc;
  2. using Microsoft.AspNetCore.Mvc.Filters;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. namespace CoreTest.Filter
  8. {
  9. public class AuthFilter : Attribute, IAuthorizationFilter
  10. {
  11. public void OnAuthorization(AuthorizationFilterContext context)
  12. {
  13. if (context.HttpContext.User.Identity.Name != "1") //只是个示范作用
  14. {
  15. //未通过验证则跳转到无权限提示页
  16. RedirectToActionResult content = new RedirectToActionResult("NoAuth", "Exception",null);
  17. context.Result = content;
  18. }
  19. }
  20. }
  21. }
复制代码

二、行为过滤器ActionFilter

  1. using Microsoft.AspNetCore.Mvc.Filters;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using ZCommon;
  8. namespace CoreTest.Filter
  9. {
  10. public class OperationLogFilter : Attribute, IActionFilter
  11. {
  12. public OperationLogFilter(string operType)
  13. {
  14. this.OperType = operType;
  15. }
  16. public string Tag { get; set; }
  17. public string OperType { get; set; }
  18. /// <summary>
  19. /// 执行前
  20. /// </summary>
  21. /// <param name="context"></param>
  22. public void OnActionExecuting(ActionExecutingContext context)
  23. {
  24. LogHelper.WriteLog("参数:" + context.HttpContext.Request.Host, LogType.remark);
  25. }
  26. /// <summary>
  27. /// 执行后
  28. /// </summary>
  29. /// <param name="context"></param>
  30. public void OnActionExecuted(ActionExecutedContext context)
  31. {
  32. }
  33. }
  34. }
复制代码

 


来源:https://blog.csdn.net/qq_26900081/article/details/83185182
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-26 13:07 , Processed in 1.956928 second(s), 25 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表