public ApiScope ToEntity(IdentityServer4.EntityFramework.Entities.ApiResource savedApi) { return(new ApiScope() { ApiResourceId = savedApi.Id, Description = Description, Required = Required, DisplayName = DisplayName, Emphasize = Emphasize, Name = Name, ShowInDiscoveryDocument = ShowInDiscoveryDocument, UserClaims = UserClaims.Select(s => new ApiScopeClaim() { Type = s }).ToList(), }); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } if (ApiResource.Id == 0) { //Hash secrets if (ApiResource?.Secrets?.Count > 0) { foreach (var secret in ApiResource.Secrets) { secret.Value = secret.Value.Sha256(); } } ApiResource.UserClaims = UserClaims.Select(c => new ApiResourceClaim() { Type = c }).ToList(); foreach (var scope in ApiResource.Scopes) { if (scope.UserClaims == null) { scope.UserClaims = new List <ApiScopeClaim>(); } var key = (ApiResource.Scopes.IndexOf(scope) + 1) * -1; if (ScopeUserClaims.ContainsKey(key.ToString())) { var claims = ScopeUserClaims[key.ToString()] as List <string>; if (claims != null && claims.Any()) { scope.UserClaims = claims.Select(c => new ApiScopeClaim() { Type = c }).ToList(); } } } _context.ApiResources.Add(ApiResource); await _context.SaveChangesAsync(); } else { var currentApiResource = _context.ApiResources .Include(c => c.Scopes).ThenInclude(s => s.UserClaims) .Include(c => c.Secrets) .Include(c => c.UserClaims) .SingleOrDefault(c => c.Id == ApiResource.Id); if (currentApiResource.Scopes == null) { currentApiResource.Scopes = new List <ApiScope>(); } foreach (var scope in currentApiResource.Scopes) { if (scope.UserClaims == null) { scope.UserClaims = new List <ApiScopeClaim>(); } } if (ApiResource.Scopes == null) { ApiResource.Scopes = new List <ApiScope>(); } foreach (var scope in ApiResource.Scopes) { if (scope.UserClaims == null) { scope.UserClaims = new List <ApiScopeClaim>(); } if (ScopeUserClaims.ContainsKey(scope.Id.ToString())) { var claims = ScopeUserClaims[scope.Id.ToString()] as List <string>; if (claims != null && claims.Any()) { scope.UserClaims = claims.Select(c => new ApiScopeClaim() { Type = c }).ToList(); } } else { var key = (ApiResource.Scopes.IndexOf(scope) + 1) * -1; if (ScopeUserClaims.ContainsKey(key.ToString())) { var claims = ScopeUserClaims[key.ToString()] as List <string>; if (claims != null && claims.Any()) { scope.UserClaims = claims.Select(c => new ApiScopeClaim() { Type = c }).ToList(); } } } } Mapper.Map(ApiResource, currentApiResource); currentApiResource.Scopes.ReflectToEntityFrameworkState(ApiResource.Scopes, _context); currentApiResource.Secrets.ReflectToEntityFrameworkState(ApiResource.Secrets, _context); currentApiResource.UserClaims.ReflectToEntityFrameworkState(UserClaims?.Select(c => new ApiResourceClaim() { Type = c }).ToList(), _context); await _context.SaveChangesAsync(); } return(RedirectToPage("./List")); }