asp.net-mvc – 防止在ASP.NET MVC中缓存属性,每次执行一个Action时强制执行属性
发布时间:2021-02-21 07:01:20 所属栏目: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 – SSL安全SaaS应用程序的URL设计
- iis-7.5 – 使用虚拟目录/应用程序在IIS中托管ASP.NET 5 We
- 如何以编程方式将ListItems添加到ASP.NET中的DropDownList?
- 我应该在ASP.NET MVC中构建我的下一个Web应用程序吗?
- asp.net 将一个图片以二进制值的形式存入Xml文件中的实例代
- asp.net-mvc-3 – 如何从ASP.NET MVC#输出中删除空格?
- 并行运行ASP.NET Webforms和ASP.NET MVC
- asp.net-mvc – MVC4部分视图没有将值加载到“容器”模型中
- asp.net-mvc – 在ASP.NET MVC中添加服务引用4
- asp.net-mvc – 为什么我的视图模型中的内部成员无法在视图
推荐文章
站长推荐
- asp.net – 如何序列化LINQ-to-SQL惰性列表
- asp.net-ajax – Ajax脚本管理器和母版页
- asp.net-mvc – 如何在asp.net mvc中处理分页?
- asp.net-mvc – ASP.NET MVC WebSite中的ERR_EMP
- ASP.net MVC ValidationSummary总是被渲染
- asp.net-mvc-routing – 在MVC 6控制器中使用url
- asp.net中使用repeater和PageDataSource搭配实现
- 什么是使用aspnet_compiler.exe预编译ASP.NET项目
- asp.net-mvc-3 – Telerik MVC网格,在运行时从集
- asp.net – 如何从WCF客户端拦截raw soap reques
热点阅读