public async Task Invoke(HttpContext context)
        {
            var cacheKey = CacheKey.With(GetType(), "GetAllPlatformRoles");

            try
            {
                await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
                {
                    cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeToken());
                    var allRolesIds = (await _platformSecurityApi.SearchRolesAsync(new RoleSearchCriteria {
                        Take = int.MaxValue
                    })).Roles.Select(x => x.Id).ToArray();
                    foreach (var role in SecurityConstants.Roles.AllRoles)
                    {
                        if (!allRolesIds.Contains(role.Id))
                        {
                            await _platformSecurityApi.UpdateRoleAsync(role.ToRoleDto());
                        }
                    }
                    return(allRolesIds);
                }, cacheNullValue : false);
            }
            catch (Exception ex)
            {
                _looger.LogError(ex, ex.Message);
            }

            await _next(context);
        }
Пример #2
0
        public override async Task <Role> FindByIdAsync(string roleId)
        {
            var cacheKey = CacheKey.With(GetType(), "FindByIdAsync", roleId);
            var result   = await _memoryCache.GetOrCreateExclusiveAsync(cacheKey, async (cacheEntry) =>
            {
                cacheEntry.AddExpirationToken(SecurityCacheRegion.CreateChangeToken());
                var role = await base.FindByIdAsync(roleId);
                if (role != null)
                {
                    await LoadRolePermissionsAsync(role);
                }
                return(role);
            }, cacheNullValue : false);

            return(result);
        }