zoukankan      html  css  js  c++  java
  • 自定义授权筛选器

    Demo

        [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
        public class AdminAuthorizeAttribute : FilterAttribute, IAuthorizationFilter
        {
            public void OnAuthorization(AuthorizationContext filterContext)
            {
                if (SkipAuthorization(filterContext))
                {
                    return;
                }
                if (filterContext == null)
                    throw new ArgumentNullException("filterContext");
    
                //判断是否已登陆
                if (HttpContext.Current == null ||
                    !HttpContext.Current.Request.IsAuthenticated ||
                    !(HttpContext.Current.User.Identity is FormsIdentity))
                {
                    filterContext.Result = new HttpUnauthorizedResult();
                }
                else
                {
                    //判断角色权限
                    var authenticationService = DependencyResolver.Current.GetService<IFormsAuthenticationService>();
                    var roleRelationFunction = new List<CustomerRoleFunction>();
                    var controllerName = filterContext.RouteData.Values["controller"].ToString();
                    var actionName = filterContext.RouteData.Values["action"].ToString();
                    var customer = authenticationService.GetCustomer();
                    if (customer != null)
                    {
                        roleRelationFunction.AddRange(customer.CustomerRoles.SelectMany(roles => roles.CustomerRoleFunctions));
                    }
                    if (!roleRelationFunction.Any(c => c.ActionName == actionName && c.ControllerName == controllerName))
                    {
                        HandleUnauthorizedRequest(filterContext, "你无此权限,如需要请通知管理员添加,点击返回");
                    }
                }
            }
    
            private void HandleUnauthorizedRequest(AuthorizationContext filterContext, string message)
            {
                var content = new ContentResult
                {
                    Content = string.Format("<a href='javascript:history.go(-1);'>{0}</a>", message)
                };
                filterContext.Result = content;
            }
    
            /// <summary>
            /// 过滤 AllowAnonymousAttribute 特性
            /// </summary>
            /// <param name="filterContext"></param>
            /// <returns></returns>
            private static bool SkipAuthorization(AuthorizationContext filterContext)
            {
                Contract.Assert(filterContext != null);
    
                return filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Any()
                       || filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), true).Any();
            }
        }
  • 相关阅读:
    操作系统丶并发并行和线程
    网络基础补充,断点续传,以及如何添加进度条
    python小游戏之贪吃蛇
    python2和3的区别丶网络编程以及socketserver多线程
    面向对象之套接字(socket)和黏包
    面向对象多继承和网络编程
    约束,自定义异常,加密,日志
    方法和函数,isinstance/issubclass/type以及反射
    面向对象之组合的补充,主动调用其他类的成员,特殊成员
    关于卡尔曼滤波和粒子滤波最直白的解释
  • 原文地址:https://www.cnblogs.com/ideacore/p/7600850.html
Copyright © 2011-2022 走看看