Ejemplo n.º 1
0
        /// <summary>
        /// Note these claims don't get saved in the cookie.
        /// </summary>
        public async Task <ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal)
        {
            if (!principal.Identity.IsAuthenticated || _isTransformed)
            {
                return(await Task.FromResult(principal));
            }
            var currentPrincipal = (ClaimsIdentity)principal.Identity;
            var currentClaims    = currentPrincipal.Claims.ToList();

            var nameIdentifier = Guid.Parse(principal.FindFirstValue(ClaimTypes.NameIdentifier));

            if (!Cache.TryGetValue(nameIdentifier, out List <Claim> claims))
            {
                claims = await ClaimsService.GenerateClaims(currentClaims);

                Cache.Set(nameIdentifier, claims, DateTimeOffset.UtcNow.Add(ClaimCachePeriod));
            }
            currentPrincipal.AddClaims(claims);
            _isTransformed = true;
            return(await Task.FromResult(principal));
        }
Ejemplo n.º 2
0
 public ClaimsTransformer(IMemoryCache cache, ClaimsService claimsService, IConfiguration configuration)
 {
     Cache            = cache;
     ClaimsService    = claimsService;
     ClaimCachePeriod = TimeSpan.Parse(configuration.GetNonEmptyValue("ClaimsCachePeriod"));
 }