asp.net-mvc – 防止在ASP.NET MVC中缓存属性,每次执行一个Action时强制执行属性
发布时间:2021-02-21 09:03:52 所属栏目: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-core – ASP.Net 5类库中的EntityFramework命令?
- 在IIS / ASP.Net中的.NET 1.1应用程序中创建.NET 3.0子应用
- 如何将数组从Asp.net服务器端传递到客户端的Javascript函数
- asp.net – 实体框架CTP5,代码优先.可选的导航属性
- ASP.NET成员资格 – 让用户使用以前的密码
- asp.net-web-api – 在ASP.NET Web API控制器的nunit测试中
- 实体框架 – 使用EF和WebAPI,如何返回一个ViewModel并支持I
- asp.net – 哪里是.ASPXAUTH cookie
- asp.net-mvc – asp.net MVC应该是View-Model封装域模型吗?
- asp.net – WebForms:MasterPages中的动态(或绝对)脚本标记
推荐文章
站长推荐
- asp.net-mvc – asp.net mvc – string或int的路
- Asp.Net超大文件上传问题解决
- asp.net-web-api – Web Api:找不到System.Net.
- asp.net – 如何MSDeploy构建的网站包到一个处女
- 在使用ASP.NET会话时是否可以强制请求并发?
- asp.net-mvc – 为什么在我的ASP MVC4应用程序中
- asp.net-mvc – 如何使用selenium进行ASP.NET MV
- asp.net-mvc – 使用MVC3剃刀的ASP.Net图表控件
- asp.net-mvc – URL中的ASP.NET MVC冒号
- asp.net-mvc – 使用没有ORM的ASP.NET MVC
热点阅读
