asp.net-mvc – 防止在ASP.NET MVC中缓存属性,每次执行一个Actio
发布时间:2020-12-15 19:55:38 所属栏目:asp.Net 来源:网络整理
导读:根据各种文章(例如 here和 here),可以缓存ASP.NET MVC操作的属性结果,并且在调用控制器操作时不再执行. 在我的情况下,这种行为是不可取的(例如,我有一个基于我自己的属性和IP的授权系统,每次都需要执行的角色检查,以及其他事情). 如何阻止ASP.NET MVC缓存我
根据各种文章(例如
here和
here),可以缓存ASP.NET MVC操作的属性结果,并且在调用控制器操作时不再执行.
在我的情况下,这种行为是不可取的(例如,我有一个基于我自己的属性和IP的授权系统,每次都需要执行的角色检查,以及其他事情). 如何阻止ASP.NET MVC缓存我的属性/属性执行结果并保证每次都执行它们? 解决方法
查看AuthorizeAttribute的源代码(在Codeplex上或通过Reflector),了解如何关闭授权页面的缓存.我将它重构为我的自定义授权属性的单独方法,该属性派生自AuthorizeAttribute.
protected void CacheValidateHandler( HttpContext context,object data,ref HttpValidationStatus validationStatus ) { validationStatus = OnCacheAuthorization( new HttpContextWrapper( context ) ); } protected void SetCachePolicy( AuthorizationContext filterContext ) { // ** IMPORTANT ** // Since we're performing authorization at the action level,the authorization code runs // after the output caching module. In the worst case this could allow an authorized user // to cause the page to be cached,then an unauthorized user would later be served the // cached page. We work around this by telling proxies not to cache the sensitive page,// then we hook our custom authorization code into the caching mechanism so that we have // the final say on whether a page should be served from the cache. HttpCachePolicyBase cachePolicy = filterContext.HttpContext.Response.Cache; cachePolicy.SetProxyMaxAge( new TimeSpan( 0 ) ); cachePolicy.AddValidationCallback( CacheValidateHandler,null /* data */); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – VIEWDATA和VIEWBAG存储在MVC中的哪个位置?
- asp.net – Web部署项目构建不再属于项目的文件
- asp.net – 如何在主管批准更改之前保留新旧版本的记录?
- ASP.NET 5/ASP.NET Core中的关注点和n层架构的分离1
- asp.net – 获取Gridview中隐藏列的值
- asp.net-core-mvc – 将复杂对象存储在MVC 6中的TempData中
- asp.net-mvc – 将返回文件的长时间运行进程
- ASP.NET -- WebForm -- ViewState
- asp.net – 虽然会话丢失,但Owin仍会对用户进行身份验证
- asp.net-web-api – 标题中的API密钥与swashbuckle
推荐文章
站长推荐
- 手动升级后,将新的ASP.NET Web Optimization框架
- asp.net – ReportViewer 2010不从代码加载数据源
- .net – 即时生成站点地图
- asp.net-mvc – Controller Action方法仅在第一次
- Asp.NET Core+ABP框架+IdentityServer4+MySQL+Ex
- 突出显示ASP.Net菜单中的选定选项卡
- 用户锁定.net 4.5.1 ASP.NET MVC 5
- .Net Core微服务入门全纪录(八)——Docker Com
- asp.net-mvc – ASPNET MVC中的Modelbinding数据
- asp.net – 从URLReferrer获取querystring
热点阅读