// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, HttpClient client) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseAuthentication(); app.UseMvc(); bool isSuccess = ClaimsAnalyzer.SendClaimToIdentityServer(client, "http://localhost:5000/api/claims", Services.Constants.IDENTITY); if (!isSuccess) { //TODO log it } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, HttpClient client) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseAuthentication(); app.UseStaticFiles(); app.UseMvcWithDefaultRoute(); //IRouter routeCollection = null; //app.UseMvc(routes => //{ // routes.MapRoute( // name: "default", // template: "{controller=Home}/{action=Index}/{id?}"); // routeCollection = routes.Build(); //}); //RouteData data = new RouteData(); //data.Routers.Add(routeCollection); //ActionContext context = new ActionContext(new DefaultHttpContext(),data, // new ActionDescriptor() { }); ////context.RouteData.Routers = new List<IRouter> { routeCollection }; //var url = urlHelper.GetUrlHelper(context); ////menuService.GenerateMenusByControllerAction(); bool isSuccess = ClaimsAnalyzer.SendClaimToIdentityServer(client, "http://localhost:5000/api/claims", Services.Constants.Identity); if (!isSuccess) { //TODO log it } }
/// <summary> /// 从 Controller/Action 中生成菜单数据 /// </summary> /// <param name="isSync">是否同步。True 表示同步,即删除原先的菜单数据重新生成。默认为 False,如果菜单数据已经存在,则直接返回。</param> /// <returns></returns> public List <MenuDTO> GenerateMenusByControllerAction(bool isSync = false) { //TODO, lock may affect the performance lock (_lockObj) { IUnitOfWork unitOfWork; using (unitOfWork = _unitOfWorkManager.Begin()) { var menuRepo = unitOfWork.GetRepository <MenuDTO, int>(); List <MenuDTO> menus; if (!isSync) { menus = menuRepo.GetAll().ToList(); if (menus != null && menus.Any()) { return(menus); } } //如果同步,则删除原先的数据重新生成。 menuRepo.RemoveAll(); Assembly assembly = Assembly.GetEntryAssembly(); menus = new List <MenuDTO>(); Regex regexClaimPolicy = new Regex(ClaimConstants.CLAIM_REGULAR_PATTERN); var controlleractionlist = ClaimsAnalyzer.GetControllerActionList(assembly); controlleractionlist.ForEach(action => { //获得 action 级别的 attribute IEnumerable <AuthorizeAttribute> controllerLevelAuthorizeAttrs = action.DeclaringType.GetCustomAttributes <AuthorizeAttribute>(true); IEnumerable <AuthorizeAttribute> actionLevelAuthorizeAttrs = action.GetCustomAttributes <AuthorizeAttribute>(true); //bool accessAllowed = true; List <Claim> claims = GetClaimsFromAuthorizeAttribute(controllerLevelAuthorizeAttrs, regexClaimPolicy); claims.AddRange(GetClaimsFromAuthorizeAttribute(actionLevelAuthorizeAttrs, regexClaimPolicy)); string controllerName = GetControllerName(action.DeclaringType); MenuDTO menu = new MenuDTO { Name = controllerName + action.Name, Url = _url.Action(action.Name, controllerName), Order = 0, Claims = String.Join(GeneralConstants.DelimeterSemicolon, claims ?? new List <Claim>()), Visible = true }; menus.Add(menu); menuRepo.AddRange(menus); }); unitOfWork.Commit(); return(menus); } } }