コード例 #1
0
        /// <summary>授权发生时触发</summary>
        /// <param name="filterContext"></param>
        public void OnAuthorization(AuthorizationFilterContext filterContext)
        {
            /*
             * 验证范围:
             * 1,魔方区域下的所有控制器
             * 2,所有带有EntityAuthorize特性的控制器或动作
             */
            var act  = filterContext.ActionDescriptor;
            var ctrl = (ControllerActionDescriptor)act;

            // 允许匿名访问时,直接跳过检查
            if (
                ctrl.MethodInfo.IsDefined(typeof(AllowAnonymousAttribute)) ||
                ctrl.ControllerTypeInfo.IsDefined(typeof(AllowAnonymousAttribute)))
            {
                return;
            }

            // 如果控制器或者Action放有该特性,则跳过全局
            var hasAtt =
                ctrl.MethodInfo.IsDefined(typeof(EntityAuthorizeAttribute), true) ||
                ctrl.ControllerTypeInfo.IsDefined(typeof(EntityAuthorizeAttribute));

            if (IsGlobal && hasAtt)
            {
                return;
            }

            // 只验证管辖范围
            var create = false;

            if (!AreaBase.Contains(ctrl))
            {
                if (!hasAtt)
                {
                    return;
                }
                // 不属于魔方而又加了权限特性,需要创建菜单
                create = true;
            }

            // 根据控制器定位资源菜单
            var menu = GetMenu(filterContext, create);

            // 如果已经处理过,就不处理了
            if (filterContext.Result != null)
            {
                return;
            }

            if (!AuthorizeCore(filterContext.HttpContext))
            {
                HandleUnauthorizedRequest(filterContext);
            }
        }
コード例 #2
0
ファイル: CubeService.cs プロジェクト: 31833077/NewLife.Cube
        /// <summary>使用魔方</summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCube(this IApplicationBuilder app)
        {
            // 配置静态Http上下文访问器
            app.UseStaticHttpContext();

            var set = Setting.Current;

            // 压缩配置
            if (set.EnableCompress)
            {
                app.UseResponseCompression();
            }

            // 注册中间件
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();

            //app.UseMiddleware<ErrorMiddleware>();
            app.UseMiddleware <RunTimeMiddleware>();
            if (set.WebOnline || set.WebBehavior || set.WebStatistics)
            {
                app.UseMiddleware <UserBehaviorMiddleware>();
            }

            if (set.SslMode > SslModes.Disable)
            {
                app.UseHttpsRedirection();
            }

            app.UseAuthentication();

            app.UseRouting();
            // 设置默认路由
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    "CubeAreas",
                    "{area=Admin}/{controller=Index}/{action=Index}/{id?}");
                endpoints.MapControllerRoute(
                    "Default",
                    "{controller=Index}/{action=Index}/{id?}"
                    );
                endpoints.MapRazorPages();
            })
            .Build();

            // 使用管理提供者
            app.UseManagerProvider();

            // 自动检查并添加菜单
            AreaBase.RegisterArea <Admin.AdminArea>();

            return(app);
        }
コード例 #3
0
        /// <summary>使用魔方</summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCube(this IApplicationBuilder app, IWebHostEnvironment env = null)
        {
            // 配置静态Http上下文访问器
            app.UseStaticHttpContext();

            var set = Setting.Current;

            // 压缩配置
            if (set.EnableCompress)
            {
                app.UseResponseCompression();
            }

            // 注册中间件
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();

            //app.UseMiddleware<ErrorMiddleware>();
            app.UseMiddleware <RunTimeMiddleware>();
            if (set.WebOnline || set.WebBehavior || set.WebStatistics)
            {
                app.UseMiddleware <UserBehaviorMiddleware>();
            }

            if (set.SslMode > SslModes.Disable)
            {
                app.UseHttpsRedirection();
            }

            app.UseAuthentication();

            app.UseRouting();
            // 设置默认路由
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    "CubeAreas",
                    "{area}/{controller=Index}/{action=Index}/{id?}");
                endpoints.MapControllerRoute(
                    "Default",
                    "{controller=CubeHome}/{action=Index}/{id?}"
                    );
                endpoints.MapRazorPages();
            })
            .Build();

            // 使用管理提供者
            app.UseManagerProvider();

            // 自动检查并添加菜单
            AreaBase.RegisterArea <Admin.AdminArea>();

            // 使用Cube前添加自己的管道
            if (env != null)
            {
                if (!env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/CubeHome/Error");
                }
            }

            return(app);
        }
コード例 #4
0
        /// <summary>使用魔方</summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCube(this IApplicationBuilder app)
        {
            //var loggerFactory = app.ApplicationServices.GetService(typeof(ILoggerFactory)) as ILoggerFactory;
            //loggerFactory.CreateLogger("");

            // 配置静态Http上下文访问器
            app.UseStaticHttpContext();

            var set = Setting.Current;

            // 添加自定义中间件
            // 注册错误处理模块中间件
            app.UseErrorModule();

            // 压缩配置
            if (set.EnableCompress)
            {
                app.UseResponseCompression();
            }

            // 注册请求执行时间中间件
            app.UseDbRunTimeModule();

            if (set.SslMode > SslModes.Disable)
            {
                app.UseHttpsRedirection();
            }

            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();
            app.UseAuthentication();

            app.UseMvc(routes =>
            {
                //var builder = new ODataConventionModelBuilder();
                //builder.EntitySet<UserX>("UserXs");
                //// OData路由放在最前面
                //routes.MapODataServiceRoute("ODataRoute","OData", builder.GetEdmModel());

                // 区域路由注册
                routes.MapRoute(
                    name: "CubeAreas",
                    template: "{area=Admin}/{controller=Index}/{action=Index}/{id?}"
                    );

                // 为魔方注册默认首页,启动魔方站点时能自动跳入后台,同时为Home预留默认过度视图页面
                routes.MapRoute(
                    name: "Cube",
                    template: "{controller=CubeHome}/{action=Index}/{id?}"
                    );
            });

            // 使用管理提供者
            app.UseManagerProvider();

            // 自动检查并添加菜单
            //XTrace.WriteLine("初始化权限管理体系");
            //var user = ManageProvider.User;
            //ManageProvider.Provider.GetService<IUser>();
            //ScanControllerExtensions.ScanController();
            AreaBase.RegisterArea <Admin.AdminArea>();

            return(app);
        }
コード例 #5
0
        /// <summary>使用魔方,放在UseEndpoints之前,自动探测是否UseRouting</summary>
        /// <param name="app"></param>
        /// <param name="env"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseCube(this IApplicationBuilder app, IWebHostEnvironment env)
        {
            XTrace.WriteLine("{0} Start 初始化魔方 {0}", new String('=', 32));

            var set = Setting.Current;

            // 使用Cube前添加自己的管道
            if (env != null)
            {
                // 使用自己的异常处理页,后续必须再次UseRouting
                if (!env.IsDevelopment())
                {
                    app.UseExceptionHandler("/CubeHome/Error");
                }
            }

            // 设置X-Frame-Options
            app.Use(async(context, next) =>
            {
                if (!set.XFrameOptions.IsNullOrWhiteSpace())
                {
                    context.Response.Headers[HeaderNames.XFrameOptions] = set.XFrameOptions;
                }

                await next();
            });

            if (!set.CorsOrigins.IsNullOrEmpty())
            {
                app.UseCors("cube_cors");
            }

            // 配置静态Http上下文访问器
            app.UseStaticHttpContext();

            // 注册中间件
            //app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();

            if (TracerMiddleware.Tracer != null)
            {
                app.UseMiddleware <TracerMiddleware>();
            }
            app.UseMiddleware <RunTimeMiddleware>();

            if (env != null)
            {
                app.UseCubeDefaultUI(env);
            }

            // 设置默认路由。如果外部已经执行 UseRouting,则直接注册
            app.UseRouter(endpoints =>
            {
                XTrace.WriteLine("注册魔方区域路由");

                endpoints.MapControllerRoute(
                    "CubeAreas",
                    "{area}/{controller=Index}/{action=Index}/{id?}");
            });

            // 使用管理提供者
            app.UseManagerProvider();

            // 自动检查并添加菜单
            AreaBase.RegisterArea <Admin.AdminArea>();

            XTrace.WriteLine("{0} End   初始化魔方 {0}", new String('=', 32));

            return(app);
        }