public OperationDataResult <Dictionary <string, string> > GetCurrentUserClaims() { try { var result = new Dictionary <string, string>(); var userId = _userService.UserId; if (userId < 1) { throw new Exception("Current user not found!"); } var auth = _authCacheService.TryGetValue(_userService.UserId); if (auth == null) { // TODO if user info is null return(new OperationDataResult <Dictionary <string, string> >(true, result)); } foreach (var authClaim in auth.Claims) { result.Add(authClaim.Type, authClaim.Value); } return(new OperationDataResult <Dictionary <string, string> >(true, result)); } catch (Exception e) { _logger.LogError(e.Message); return(new OperationDataResult <Dictionary <string, string> >(false, e.Message)); } }
public async Task <ClaimsPrincipal> TransformAsync(ClaimsPrincipal principal) { // create a copy var cp = principal.Clone(); // get Identity var ci = (ClaimsIdentity)cp.Identity; var list = ci.Claims.Select(x => new { key = x.Type, value = x.Value }).ToList(); var userIdClaim = list.FirstOrDefault(x => x.key == ClaimTypes.NameIdentifier); var timeClaim = list.FirstOrDefault(x => x.key == AuthConsts.ClaimLastUpdateKey); if (userIdClaim == null) { throw new Exception("user claim not found"); } if (timeClaim == null) { throw new Exception("time claim not found"); } var userId = int.Parse(userIdClaim.value); // try to get user info from memory storage var auth = _authCacheService.TryGetValue(userId); if (auth == null) { _httpContextAccessor.HttpContext.Response.Headers.Add(AuthConsts.UpdateHeaderName, AuthConsts.UpdateHeaderValue); } else { // check timestamp var timeValue = long.Parse(timeClaim.value); if (timeValue != auth.TimeStamp) { _httpContextAccessor.HttpContext.Response.Headers.Add(AuthConsts.UpdateHeaderName, AuthConsts.UpdateHeaderValue); } // Add claims foreach (var authClaim in auth.Claims) { ci.AddClaim(authClaim); } } return(await Task.FromResult(cp)); }
public async Task UpdateAuthenticatedUsers(List <int> securityGroups) { try { var groupUsers = await _dbContext.SecurityGroupUsers .AsNoTracking() .Where(x => securityGroups.Contains(x.SecurityGroupId)) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Select(x => new { UserId = x.EformUserId, Role = x.EformUser.UserRoles .Select(y => y.Role.Name) .FirstOrDefault(), }) .ToListAsync(); foreach (var user in groupUsers) { // try to get auth item var auth = _authCacheService.TryGetValue(user.UserId); if (auth != null) { var isAdmin = user.Role == EformRole.Admin; var timeStamp = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(); var claims = await GetUserPermissions(user.UserId, isAdmin); auth.Claims = claims; auth.TimeStamp = timeStamp; _authCacheService.Set(auth, user.UserId); } } } catch (Exception e) { _logger.LogError(e.Message); throw; } }