public string GetToken(PushConfig config, string source = "") { object token = null; if (config.EnableTokenCache.ToLower() == "true") { token = cache.Get <object>(config.Value); } if (token == null) { lock (cacheLock) { if (token == null) { string GetTokenUrl = config.TokenUrl; string appId = config.AppId; string appSecret = config.AppSecret; long t = DateTimeOffset.Now.ToUnixTimeSeconds(); // 相差秒数 var key = ToMD5(appId + appSecret + t); var p = JsonConvert.SerializeObject(new { AppID = appId, t = t.ToString(), sign = key }); string res = string.Empty; if (source.Contains("api")) { res = PostHttps(GetTokenUrl, p); } else { res = Post(GetTokenUrl, p); } if (res.IndexOf("ErrorCode") < 0) { //成功 var info = JsonConvert.DeserializeObject <dynamic>(res); token = info.access_token; if (config.EnableTokenCache.ToLower() == "true") { var expires = Convert.ToInt32(info.expires_in);//token 过期时间 var st = DateTime.Now.AddSeconds(expires); cache.Insert(config.Value, token, st); } } else { //错误,记录日志 throw new Exception("获取Token出错,错误信息:" + res + "; 请求参数,1_GetTokenUrl:" + GetTokenUrl + " 2_p:" + p + ";config:" + JsonConvert.SerializeObject(config)); } } } } return(token.ToString()); }
public void SetCategoryList(object category = null) { if (!_cacheManager.IsSet("CategoriList_")) { _cacheManager.Add("CategoriList_", _categoryRepository.GetMany(x => x.ParentCategoryId == 0).ToList(), 3600); } var categoryList = _cacheManager.Get <List <Category> >("CategoriList_"); ViewBag.CategoryList = categoryList; }
public static T Get <T>(this IMemoryCacheManager cacheManager, string key, Func <T> acquire, int cacheTime = 60) { if (cacheManager.IsSet(key)) { return(cacheManager.Get <T>(key)); } else { var result = acquire(); cacheManager.Set(key, result, cacheTime); return(result); } }
public IEnumerable <RoleMenuModel> GetCurrentUserRoleMenu() { Guid userId = this.GetCurrentUserId(); var cache = _memoryCacheManager.Get(MemoryCacheKey.CURRENT_USER_ROLE_MENU, userId, () => { var data = this._repoRoleMenu.Query() .Filter(x => x.sys_Role.sys_UserRoles.Any(ur => ur.userId == userId && !ur.deleted)) .Include(x => x.sys_Menu) .Get() .ToList(); List <RoleMenuModel> result = new List <RoleMenuModel>(); var parents = data.Where(x => !x.parentRoleMenuId.HasValue) .GroupBy(p => new { description = string.IsNullOrEmpty(p.description) ? p.sys_Menu.description : p.description, actionName = p.sys_Menu.actionName, controllerName = p.sys_Menu.controllerName, areaName = p.sys_Menu.areaName, parameter = p.sys_Menu.parameter, menuId = p.sourceMenuId, mainMenu = true, }) .Select(x => { return(new RoleMenuModel() { description = x.Key.description, actionName = x.Key.actionName, controllerName = x.Key.controllerName, parameter = x.Key.parameter, areaName = x.Key.areaName, menuId = x.Key.menuId, mainMenu = x.Key.mainMenu, Childs = _RoleMenuGetChildsWithGroupings(data, x.Select(s => s.id)), displayOrder = x.Min(m => m.displayOrder) }); }) .OrderBy(x => x.displayOrder) .ThenBy(x => x.description) ; result = parents.ToList(); return(result); }); return(cache); }
public async Task <T> GetItemFromHashAsync <T>(string redisKey, string redisHashKey) { var compareKey = GenerateKey(redisKey + redisHashKey); if (_memoryCacheManager.IsSet(compareKey)) { return(_memoryCacheManager.Get <T>(compareKey)); } var rValue = await _db.HashGetAsync(GenerateKey(redisKey), redisHashKey); if (!rValue.HasValue) { _memoryCacheManager.Set(compareKey, 0, MemoryCacheTime); return(default(T)); } var resultValue = Newtonsoft.Json.JsonConvert.DeserializeObject <T>(rValue); _memoryCacheManager.Set(compareKey, resultValue, MemoryCacheTime); return(Newtonsoft.Json.JsonConvert.DeserializeObject <T>(rValue)); }
public async Task <AccessTokenResult> GetAccessToken() { var cacheKey = "_GraphToken"; var graphToken = _memoryCacheManager.Get <AccessTokenResult>(cacheKey); if (!string.IsNullOrEmpty(graphToken?.Access_Token)) { return(graphToken); } var clientCredentials = new ClientCredential(_client_secret); var app = new ConfidentialClientApplication(_client_id, $"{_host}/{_tenant}", "https://graph.microsoft.com", clientCredentials, null, new TokenCache()); // With client credentials flows the scopes is ALWAYS of the shape "resource/.default", as the // application permissions need to be set statically (in the portal or by PowerShell), and then granted by // a tenant administrator var scopes = new string[] { _scope }; AuthenticationResult result = null; try { result = await app.AcquireTokenForClientAsync(scopes); } catch (MsalServiceException ex) when(ex.Message.Contains("AADSTS70011")) { // Invalid scope. The scope has to be of the form "https://resourceurl/.default" // Mitigation: change the scope to be as expected } var token = new AccessTokenResult { Access_Token = result.AccessToken, Expires_In = result.ExpiresOn.ToString(), }; var cacheTime = result.ExpiresOn - DateTime.UtcNow; //cacheTime = TimeSpan.FromMinutes(5); _memoryCacheManager.Set(cacheKey, token, cacheTime); return(token); }