protected List <MenuControlAttribute> GetAdminContrller() { var cacheMenu = CacheExtends.GetDataFromCache <List <MenuControlAttribute> >(CacheExtends.DefaultAdminController); if (cacheMenu != null) { return(cacheMenu); } else { var cusMenus = new List <MenuControlAttribute>(); var assembly = Assembly.GetAssembly(typeof(AdminController)).GetTypes() .Where(o => o.BaseType.FullName == typeof(AdminController).FullName) .ToList(); int count = assembly != null ? assembly.Count : 0; for (int i = 0; i < count; i++) { var item = assembly[i]; var attribute = item.GetCustomAttribute <MenuControlAttribute>(); if (attribute == null) { continue; } var getmetho = item.GetMethods(); var method = getmetho .Where(o => o.Module.Name == typeof(AdminController).Module.Name) .Select(o => o.Name).ToList(); if (method != null) { foreach (var m in method) { attribute.ActionName.Add(m.ToLower()); } } attribute.Color = string.IsNullOrEmpty(attribute.Color) ? new GetColor().RandomColor() : attribute.Color; cusMenus.Add(attribute); } CacheExtends.SetObjectFromCache(CacheExtends.DefaultAdminController, 360 * 24 * 60, cusMenus); return(cusMenus); } }
public string RandomColor() { var _color = new List <string>(); var cacle = CacheExtends.GetDataFromCache <List <string> >("colorGet"); if (_color.Count == 0) { _color = cacle != null && cacle.Count > 0? cacle : new List <string>() { "cyan", "red", "emerald", "indigo", "violet", "teal" }; } Random random = new Random(); int id = random.Next(0, _color.Count); var colorReturn = _color[id].ToString(); _color.Remove(colorReturn); CacheExtends.SetObjectFromCache("colorGet", 240, _color); return(colorReturn); }
/// <summary> /// Render html partialView with data from db /// </summary> /// <param name="partialView"></param /// <param name="ctrl"></param> /// <param name="properties"> key | value \r\n key | value \r\n key | value</param> /// <returns></returns> public static IHtmlContent RenderWithData(this IHtmlHelper htmlHelper, string partialView, string ctrl, List <SysTemplatePropertyEntity> properties) { string keyCache = ctrl + "_partialView_" + partialView; var cache = CacheExtends.GetDataFromCache <IHtmlContent>(keyCache); if (cache == null) { var customer = properties; var data = GetDynamicDataByActName(ctrl, customer); var html = htmlHelper.PartialAsync(partialView, data); CacheExtends.SetObjectFromCache(keyCache, 10 * 60 * 24, html.Result); return(html.Result); } else { return(cache); } }
protected void GetClientContrller(ref List <MenuControlAttribute> cusMenus, ref List <CModule> modules) { var cacheControl = CacheExtends.GetDataFromCache <List <CModule> >(CacheExtends.DefaultIsControl); var cacheMenu = CacheExtends.GetDataFromCache <List <MenuControlAttribute> >(CacheExtends.DefaultClientController); if (cacheMenu != null && cacheControl != null) { cusMenus = cacheMenu; modules = cacheControl; } else { modules = new List <CModule>(); cusMenus = new List <MenuControlAttribute>(); var assembly = Assembly.GetAssembly(typeof(ClientController)).GetTypes() .Where(o => o.BaseType.FullName == typeof(ClientController).FullName) .ToList(); int count = assembly != null ? assembly.Count : 0; for (int i = 0; i < count; i++) { var item = assembly[i]; var attribute = item.GetCustomAttribute <MenuControlAttribute>(); if (attribute == null) { continue; } if (attribute.IsControl) { var fields = item.GetFields(); List <ProperyCModule> pp = new List <ProperyCModule>(); if (fields != null) { foreach (var field in fields) { var attr = field.GetCustomAttribute <PropertyAttribute>(); string name = attr.Name; string key = field.Name; string type = attr.Type; pp.Add(new ProperyCModule(key, type, name)); } } modules.Add(new CModule() { Code = attribute.CModule, Name = attribute.Name, FullName = item.FullName, Properties = pp }); } else { var getmetho = item.GetMethods(); var method = getmetho .Where(o => o.Module.Name == typeof(ClientController).Module.Name) .Select(o => o.Name).ToList(); if (method != null) { foreach (var m in method) { attribute.ActionName.Add(m.ToLower()); } } cusMenus.Add(attribute); } } CacheExtends.SetObjectFromCache(CacheExtends.DefaultIsControl, 360 * 24 * 60, modules); CacheExtends.SetObjectFromCache(CacheExtends.DefaultClientController, 360 * 24 * 60, cusMenus); } }
private static ClaimsPrincipal GetCurrentUser(this HttpContext context, IConfiguration configuration) { string token = context.GetValue(Cookies.DefaultLogin, false); if (string.IsNullOrEmpty(token)) { return(null); } else { // neeus co cache var cache = CacheExtends.GetDataFromCache <ClaimsPrincipal>(token); if (cache != null) { return(cache); } // ko co cache var logs = new CPLoginLogService(configuration); string email = logs.GetEmailFromDb(token); if (string.IsNullOrEmpty(email)) { return(null); } else { var account = new CPUserService(configuration); var user = account.GetItemByEmail(email); if (user == null) { return(null); } else { var role = new CPRoleService(configuration); var irole = role.GetByID(user.RoleID); if (role == null) { return(null); } var claims = new List <Claim> { new Claim(ClaimTypes.Email, user.Email), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Role, irole.Code), new Claim("RoleID", irole.ID.ToString()), new Claim("UserID", user.ID) }; var claimsIdentity = new ClaimsIdentity(claims, Cookies.DefaultLogin); _ = new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTime.UtcNow.AddMinutes(Cookies.ExpiresLogin) }; ClaimsPrincipal claim = new ClaimsPrincipal(); claim.AddIdentity(claimsIdentity); CacheExtends.SetObjectFromCache(token, Cookies.ExpiresLogin, claim); return(claim); } } } }