经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
ASP.NET Core的全局拦截器(在页面回发时,如果判断当前请求不合法,不执行OnPost处理器)
来源:cnblogs  作者:三生石上(FineUI控件)  时间:2024/5/15 16:49:33  对本文有异议

ASP.NET Core RazorPages中,我们可以在页面模型基类中重载OnPageHandlerExecuting方法。

下面的例子中,BaseModel继承自 PageModel,是所有页面模型的基类。

推荐方案:
在BaseModel.cs中,重载OnPageHandlerExecuting方法(看下面代码中的注释):

  1. public override void OnPageHandlerExecuting(PageHandlerExecutingContext context)
  2. {
  3. base.OnPageHandlerExecuting(context);
  4.  
  5. if (IsPostBack)
  6. {
  7. // 回发请求时,检索请求数据或者Cookie,来验证当前访问是否有效。请求无效时,弹出错误提示,不再执行Page_Load和回发事件。
  8. if (!String.IsNullOrEmpty(Request.Query["error"]))
  9. {
  10. ShowNotify("身份验证失败!");
  11.  
  12. // Setting Result to a non-null value inside a page filter will short-circuit the page and any remaining page filters.
  13. // 设置context.Result=UIHelper.Result(),可以中断页面继续执行(跳过接下来的 Page_Load 和回发事件)。
  14. context.Result = UIHelper.Result();
  15. }
  16. }
  17. }

 

微软官方文档:https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.filters.pagehandlerexecutingcontext?view=aspnetcore-8.0

上述文档中,在解释 Result 属性时,专门提到了这个事情:

 在页面过滤器内将 Result 设置为非空值将使该页面和任何剩余的页面过滤器短路。

 

延伸阅读
===================
其实上述方案在6年前发布 AppBoxCore 时已经存在了,只不过在 AppBoxCore 中做的更加工程化,也更加清晰。

当前的需求是要求有些页面需要进行身份验证,而一些公开的页面不需要身份验证。

1. 我们首先定义 CheckPowerAttribute 过滤器。

  1. namespace AppBoxCore.Dapper
  2. {
  3. /// <summary>
  4. /// AppBoxCore自定义权限验证过滤器
  5. /// </summary>
  6. public class CheckPowerAttribute : ResultFilterAttribute
  7. {
  8. /// <summary>
  9. /// 权限名称
  10. /// </summary>
  11. public string Name { get; set; }
  12. public override void OnResultExecuting(ResultExecutingContext filterContext)
  13. {
  14. HttpContext context = filterContext.HttpContext;
  15. // 权限验证不通过
  16. if (!String.IsNullOrEmpty(Name) && !BaseModel.CheckPower(context, Name))
  17. {
  18. if (context.Request.Method == "GET")
  19. {
  20. BaseModel.CheckPowerFailWithPage(context);
  21.  
  22. // -修正越权访问页面时会报错[服务器无法在发送 HTTP 标头之后追加标头](龙涛软件-9374)。
  23. filterContext.Result = new EmptyResult();
  24. }
  25. else if (context.Request.Method == "POST")
  26. {
  27. BaseModel.CheckPowerFailWithAlert();
  28. filterContext.Result = UIHelper.Result();
  29. }
  30. }
  31. }
  32.  
  33. }
  34. }

 

参考文档:http://stackoverflow.com/questions/9837180/how-to-skip-action-execution-from-an-actionfilter  

这个文档也相关有参考价值,当时遇到一个报错:

  1. 服务器无法在发送 HTTP 标头之后追加标头

如果需要在 GET 请求中结束当前请求,也需要设置 new EmptyResult()。

 

2. 然后需要进行身份验证的页面,在页面模型上定义此属性即可。
比如 Admin 目录下的 Config 页面:

  1. [CheckPower(Name = "CoreConfigView")]
  2. public class ConfigModel : BaseAdminModel
  3. {
  4. ...
  5. }

  

原文链接:https://www.cnblogs.com/sanshi/p/18193981

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号