/// <summary>
        /// Add new Api Resource Claim
        /// </summary>
        /// <param name="claim"></param>
        /// <returns></returns>
        public async Task <string> AddClaim(ApiResourceClaim claim)
        {
            string result = "";
            await Task.Run(() => {
                try {
                    using (/*var*/ ctx /*= new ResourceConfigDbContext()*/)
                    {
                        ctx.ApiClaims.Add(claim);
                        ctx.SaveChanges();
                        result = "ApiClaimId " + claim.Id.ToString();
                    }
                }
                catch (Exception ex)
                {
                    result = ex.Message;
                }
            });

            return(result);
        }
        public async Task <IActionResult> Create(ApiResourceClaimViewModel vm)
        {
            if (vm == null)
            {
                return(BadRequest());
            }

            if (ModelState.IsValid)
            {
                var apiResource = await _dbContext.ApiResources.FindAsync(vm.ApiResourceId);

                if (apiResource == null)
                {
                    return(BadRequest());
                }

                var apiResourceClaim = new ApiResourceClaim
                {
                    Type        = vm.Type,
                    ApiResource = apiResource
                };

                _dbContext.Set <ApiResourceClaim>().Add(apiResourceClaim);
                try
                {
                    await _dbContext.SaveChangesAsync();

                    _logger.LogInformation($"API resource claim Id {apiResourceClaim.Id} created by {User?.Identity?.Name}.");
                    return(RedirectToAction("Edit", "ApiResources", new { id = vm.ApiResourceId }));
                }
                catch (DbException ex)
                {
                    _logger.LogError(ex.GetBaseException()?.Message ?? ex.Message);
                    throw;
                }
            }
            return(View(vm));
        }
Exemple #3
0
        public async Task OnPostAsync()
        {
            // Arrange
            const string claim1OriginalType = "Original Type";
            const string claim1EditedType   = "Edited Type";
            const string newClaimType       = "New Type";
            var          databaseName       = $"{DatabaseNamePrefix}.{nameof(OnPostAsync)}";
            var          options            = new DbContextOptionsBuilder <OidcDbContext>()
                                              .UseInMemoryDatabase(databaseName)
                                              .Options;
            ClaimTypesModel claims;
            IActionResult   post;
            var             claim1 = new ApiResourceClaim
            {
                Id   = Random.Next(),
                Type = claim1OriginalType
            };
            var claim2 = new ApiResourceClaim {
                Id = Random.Next()
            };
            var apiResource = new ApiResource
            {
                Id         = Random.Next(),
                UserClaims = new List <ApiResourceClaim>
                {
                    claim1,
                    claim2
                }
            };

            using (var context = new OidcDbContext(options))
            {
                context.Add(apiResource);
                await context.SaveChangesAsync().ConfigureAwait(false);
            }

            // Act
            using (var context = new OidcDbContext(options))
            {
                claims = new ClaimTypesModel(context)
                {
                    ApiResource = new ApiResource
                    {
                        Id         = apiResource.Id,
                        UserClaims = new List <ApiResourceClaim>
                        {
                            new ApiResourceClaim
                            {
                                Id   = claim1.Id,
                                Type = claim1EditedType
                            },
                            new ApiResourceClaim {
                                Type = newClaimType
                            }
                        }
                    }
                };
                post = await claims.OnPostAsync().ConfigureAwait(false);
            }

            // Assert
            using (var context = new OidcDbContext(options))
            {
                apiResource = await context.ApiResources
                              .Include(x => x.UserClaims)
                              .SingleOrDefaultAsync(x => x.Id.Equals(apiResource.Id))
                              .ConfigureAwait(false);

                claim1 = apiResource.UserClaims.SingleOrDefault(x => x.Id.Equals(claim1.Id));
                claim2 = apiResource.UserClaims.SingleOrDefault(x => x.Id.Equals(claim2.Id));
                var newClaim = apiResource.UserClaims.SingleOrDefault(x => x.Type.Equals(newClaimType));

                Assert.NotNull(claim1);
                Assert.Equal(claim1EditedType, claim1.Type);
                Assert.Null(claim2);
                Assert.NotNull(newClaim);
            }

            var result = Assert.IsType <RedirectToPageResult>(post);

            Assert.Equal("../Details/ClaimTypes", result.PageName);
            Assert.Collection(result.RouteValues, routeValue =>
            {
                var(key, value) = routeValue;
                Assert.Equal(nameof(ApiResource.Id), key);
                Assert.Equal(claims.ApiResource.Id, value);
            });
        }
 public Task <string> UpdateClaim(ApiResourceClaim claim)
 {
     throw new NotImplementedException();
 }
        public static void ConfigurationDbContextSeed(this ModelBuilder modelBuilder)
        {
            var clientGrantTypeEntity = new ClientGrantType
            {
                Id        = -1,
                GrantType = GrantType.ResourceOwnerPassword,
                ClientId  = -1,
            };

            var clientScopes = new[]
            {
                new ClientScope
                {
                    ClientId = -1,
                    Id       = -1,
                    Scope    = "record-keep-api"
                },
                new ClientScope
                {
                    ClientId = -1,
                    Id       = -2,
                    Scope    = IdentityServerConstants.StandardScopes.OpenId
                },
                new ClientScope
                {
                    ClientId = -1,
                    Id       = -3,
                    Scope    = IdentityServerConstants.StandardScopes.Profile
                }
            };

            var client = new Client
            {
                ClientId                    = "record-keep",
                AccessTokenType             = AccessTokenType.Jwt,
                RequireClientSecret         = false,
                AllowOfflineAccess          = false,
                AllowAccessTokensViaBrowser = true
            };

            var clientEntity = client.ToEntity();

            clientEntity.Id = -1;

            var apiResourceClaimEntity = new ApiResourceClaim
            {
                Id            = -1,
                ApiResourceId = -1,
                Type          = JwtClaimTypes.Name
            };

            var apiResourceScopesEntity = new ApiScope
            {
                Name          = "record-keep-api",
                Id            = -1,
                DisplayName   = "Record Keep API",
                ApiResourceId = -1,
            };

            var apiResource = new ApiResource
            {
                Name        = "record-keep-api",
                DisplayName = "Record Keep API"
            };

            var apiResourceEntity = apiResource.ToEntity();

            apiResourceEntity.Id = -1;

            var openIdIdentity  = new IdentityResources.OpenId().ToEntity();
            var profileIdentity = new IdentityResources.Profile().ToEntity();

            openIdIdentity.Id  = -1;
            profileIdentity.Id = -2;

            var identityClaimOpenId = new IdentityClaim
            {
                Id   = -1,
                Type = JwtClaimTypes.Subject,
                IdentityResourceId = -1
            };

            var identityClaims = new List <IdentityClaim>
            {
                identityClaimOpenId
            };

            var index = -2;

            foreach (var claims in profileIdentity.UserClaims)
            {
                identityClaims.Add(new IdentityClaim
                {
                    Id   = index,
                    Type = claims.Type,
                    IdentityResourceId = -2,
                });

                index--;
            }

            openIdIdentity.UserClaims  = new List <IdentityClaim>();
            profileIdentity.UserClaims = new List <IdentityClaim>();

            modelBuilder.Entity <ClientGrantType>().HasData(clientGrantTypeEntity);
            modelBuilder.Entity <ClientScope>().HasData(clientScopes);
            modelBuilder.Entity <IdentityServer4.EntityFramework.Entities.Client>().HasData(clientEntity);

            modelBuilder.Entity <ApiResourceClaim>().HasData(apiResourceClaimEntity);
            modelBuilder.Entity <ApiScope>().HasData(apiResourceScopesEntity);
            modelBuilder.Entity <IdentityServer4.EntityFramework.Entities.ApiResource>().HasData(apiResourceEntity);

            modelBuilder.Entity <IdentityResource>().HasData(openIdIdentity, profileIdentity);
            modelBuilder.Entity <IdentityClaim>().HasData(identityClaims);
        }