栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在ASP.NET MVC中重定向未经授权的控制器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在ASP.NET MVC中重定向未经授权的控制器

创建基于AuthorizeAttribute的自定义授权属性,并覆盖OnAuthorization来执行检查操作的方式。通常,如果授权检查失败,则AuthorizeAttribute会将过滤结果设置为HttpUnauthorizedResult。您可以将其设置为(错误视图的)ViewResult。

编辑 :我有几个博客文章,将更详细地介绍:

  • http://farm-fresh-pre.blogspot.com/2011/03/revisiting-custom-authorization-in.html
  • http://farm-fresh-pre.blogspot.com/2009/11/customizing-authorization-in-aspnet-mvc.html

例:

    [AttributeUsage( AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false )]    public class MasterEventAuthorizationAttribute : AuthorizeAttribute    {        /// <summary>        /// The name of the master page or view to use when rendering the view on authorization failure.  Default        /// is null, indicating to use the master page of the specified view.        /// </summary>        public virtual string MasterName { get; set; }        /// <summary>        /// The name of the view to render on authorization failure.  Default is "Error".        /// </summary>        public virtual string ViewName { get; set; }        public MasterEventAuthorizationAttribute() : base()        { this.ViewName = "Error";        }        protected void CachevalidateHandler( 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 )) {     SetCachePolicy( filterContext ); } else if (!filterContext.HttpContext.User.Identity.IsAuthenticated) {     // auth failed, redirect to login page     filterContext.Result = new HttpUnauthorizedResult(); } else if (filterContext.HttpContext.User.IsInRole( "SuperUser" )) {     // is authenticated and is in the SuperUser role     SetCachePolicy( filterContext ); } else {     ViewDataDictionary viewData = new ViewDataDictionary();     viewData.Add( "Message", "You do not have sufficient privileges for this operation." );     filterContext.Result = new ViewResult { MasterName = this.MasterName, ViewName = this.ViewName, ViewData = viewData }; }        }        protected void SetCachePolicy( AuthorizationContext filterContext )        { // ** importANT ** // Since we're performing authorization at the action level, the authorization pre 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 pre 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 );        }    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/576033.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号