private async Task <List <Claim> > getClaims(string userName)
        {
            var claims = new List <Claim>();

            #region Method 1.Add extra const roles
            //claims = new List<Claim>
            //    {
            //        new Claim(JwtClaimTypes.Role, "admin"),
            //        new Claim(JwtClaimTypes.Role, "user")
            //    };
            #endregion

            #region Method 2. Add extra roles from redis
            var cacheKey = CacheKeyFactory.UserProfile(userName);
            (UserProfile user, bool isOK) = await this.cache.GetCacheAsync <UserProfile>(cacheKey);

            if (isOK)
            {
                // Role claim
                user.Roles.Split(',').Select(x => new Claim(ClaimTypes.Role, x.Trim())).ToList().ForEach(claim => claims.Add(claim));

                // Department claim
                claims.Add(new Claim(CustomClaimTypes.Department, user.Department));
            }
            #endregion

            return(claims);
        }
Пример #2
0
        public async Task <ActionResult> Remove([FromRoute] string userName)
        {
            var cacheKey = CacheKeyFactory.UserProfile(userName);

            await this.cache.ClearCacheAsync(cacheKey);

            return(this.Ok());
        }
Пример #3
0
        public async Task <ActionResult> Create([FromBody] UserProfile user)
        {
            var cacheKey = CacheKeyFactory.UserProfile(user.Username);

            await this.cache.SaveCacheAsync <UserProfile>(cacheKey, user);

            return(this.Ok());
        }
Пример #4
0
        public async Task <UserProfile> Get([FromRoute] string userName)
        {
            var cacheKey = CacheKeyFactory.UserProfile(userName);

            (UserProfile userRole, bool isOK) = await this.cache.GetCacheAsync <UserProfile>(cacheKey);

            if (!isOK)
            {
                this.HttpContext.Response.StatusCode = (int)HttpStatusCode.NoContent;
            }

            return(isOK ? userRole : null);
        }
        /// <summary>
        /// Persist
        /// </summary>
        /// <param name="evt">Event</param>
        public async Task PersistAsync(Event evt)
        {
            if (evt.Id.Equals(EventIds.UserLoginSuccess))
            {
                if (evt.EventType == EventTypes.Success || evt.EventType == EventTypes.Information)
                {
                    var httpContext = this.httpContextAccessor.HttpContext;

                    try
                    {
                        if (this.httpContextAccessor.HttpContext.Session.IsAvailable)
                        {
                            var    session  = this.httpContextAccessor.HttpContext.Session;
                            var    user     = this.httpContextAccessor.HttpContext.User;
                            var    subject  = user.Claims.Where(x => x.Type == "sub").FirstOrDefault()?.Value;
                            var    token    = session.GetString("AccessToken");
                            string cacheKey = CacheKeyFactory.UserProfile(subject);
                            _ = await this.memoryCache.GetOrCreateAsync <JObject>(cacheKey, async entry =>
                            {
                                entry.SlidingExpiration = TimeSpan.FromSeconds(600);
                                string jsonStr          = $"{{\"{subject}\":\"{token}\"}}";
                                return(await Task.FromResult(JObject.Parse(jsonStr)));
                            });

                            // Check if the cache exist
                            if (this.memoryCache.TryGetValue <JObject>(cacheKey, out JObject tokenInfo))
                            {
                                Debug.WriteLine($"Cached: {tokenInfo.ToString()}");
                            }
                        }
                    }
                    catch (Exception)
                    {
                    }
                }
                else
                {
                    this.logger.LogError($"{evt.Name} ({evt.Id}), Details: {evt.Message}");
                }
            }
        }