asp.net mvc添加到AUTHORIZE属性
发布时间:2020-05-24 05:03:03 所属栏目:asp.Net 来源:互联网
导读:如何创建自定义属性以扩展MVC中的现有Authorize属性? 从AuthorizeAttribute导出你的类.覆盖OnAuthorization方法.添加并设置CacheValidationHandler. public void CacheValidationHandler( HttpContext context, o
|
如何创建自定义属性以扩展MVC中的现有Authorize属性? 解决方法从AuthorizeAttribute导出你的类.覆盖OnAuthorization方法.添加并设置CacheValidationHandler.public void CacheValidationHandler( HttpContext context,object data,ref HttpValidationStatus validationStatus )
{
validationStatus = OnCacheAuthorization( new HttpContextWrapper( context ) );
}
public override void OnAuthorization( AuthorizationContext filterContext )
{
if (filterContext == null)
{
throw new ArgumentNullException( "filterContext" );
}
if (AuthorizeCore( filterContext.HttpContext ))
{
... your custom code ...
SetCachePolicy( filterContext );
}
else if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
// auth failed,redirect to login page
filterContext.Result = new HttpUnauthorizedResult();
}
else
{
... handle a different case than not authenticated
}
}
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( CacheValidationHandler,null /* data */);
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net 自动将汉字转换成拼音第一个字母
- asp.net-mvc – 如何构造VB.NET Windows窗体应用程序
- asp.net – 动态创建的LinkButtons的OnClick事件不起作用
- asp.net-mvc-3 – 错误:asp.net mvc3中当前上下文中不存在
- asp.net-mvc – TypeScript捆绑和分类?
- asp.net-mvc – 将动作方法参数传递给asp.net mvc中的Actio
- asp.net – 请帮我理解web.config自定义设置的type属性?
- 经典的asp字符编码
- asp.net – 向GridView Row添加ID
- asp.net-mvc-4 – 创建和编辑MVC4的相同视图
推荐文章
站长推荐
- asp.net-mvc-3 – _AppStart正在执行时无法创建存
- asp.net-mvc – 如何在MVC3中的局部视图中渲染节
- asp.net – 使用OLEDB读取CSV文件,即使连接字符串
- asp.net-mvc-3 – 如何通过HTML类(Razor语法)在M
- ASP.NET – 控制事件不在Repeater内部触发
- 在ASP.NET中添加动态控件,1.1和2.0之间有区别吗?
- asp.net mvc客户端验证
- asp.net – 使用WebMethods和session时的最佳实践
- asp.net中ScriptManager和ScriptManagerProxy之间
- asp.net-mvc-4 – Web API HTTP请求命名约定
热点阅读
