public void Update(IdentityResource obj) { var idr = DbSet.SingleOrDefault(w => w.Name == obj.Name); var newOne = obj.ToEntity(); newOne.ShallowCopyTo(idr); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { LoadLookups(); return(Page()); } IdentityResource identityResource; var isNew = IdentityResource.Id == 0; if (isNew) { identityResource = IdentityResource.ToEntity(); await _dbContext.IdentityResources.AddAsync(identityResource); } else { identityResource = await LoadIdentityResource(IdentityResource.Id); IdentityResource.ToEntity(identityResource); } await _dbContext.SaveChangesAsync(); return(isNew ? RedirectToPage("/IdentityResources/Edit", new { id = identityResource.Id }) : RedirectToPage("/IdentityResources/Index")); }
public void map_IdentityResource_to_Entity() { var model = new IdentityResource { DisplayName = NewGuidS, Description = NewGuidS, Enabled = true, Name = NewGuidS, UserClaims = new List <string> { NewGuidS }, Emphasize = true, Required = true, ShowInDiscoveryDocument = true, Properties = new Dictionary <string, string>() { { NewGuidS, NewGuidS } } }; var entity = model.ToEntity(); var actual = entity.ToModel(); model.DeepCompare(actual).Should().BeTrue(); }
public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden() { var storeHolder = GetConfigurationDocumentStoreHolder(); var visibleIdentityResource = CreateIdentityTestResource(); var visibleApiResource = CreateApiResourceTestResource(); var visibleApiScope = CreateApiScopeTestResource(); var hiddenIdentityResource = new IdentityResource { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource { Name = Guid.NewGuid().ToString(), Scopes = { Guid.NewGuid().ToString() }, ShowInDiscoveryDocument = false }; var hiddenApiScope = new ApiScope { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; using (var session = storeHolder.OpenAsyncSession()) { await session.StoreAsync(visibleIdentityResource.ToEntity()); await session.StoreAsync(visibleApiResource.ToEntity()); await session.StoreAsync(visibleApiScope.ToEntity()); await session.StoreAsync(hiddenIdentityResource.ToEntity()); await session.StoreAsync(hiddenApiResource.ToEntity()); await session.StoreAsync(hiddenApiScope.ToEntity()); await session.SaveChangesAsync(); } WaitForIndexing(storeHolder.IntegrationTest_GetDocumentStore()); WaitForUserToContinueTheTest(storeHolder.IntegrationTest_GetDocumentStore()); var store = new ResourceStore(storeHolder, FakeLogger <ResourceStore> .Create()); var resources = await store.GetAllResourcesAsync(); Assert.NotNull(resources); Assert.NotEmpty(resources.IdentityResources); Assert.NotEmpty(resources.ApiResources); Assert.NotEmpty(resources.ApiScopes); Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name); Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name); Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name); Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name); }
public async Task UpdateWithChildrens(string oldName, IdentityResource irs) { var entity = irs.ToEntity(); var savedIr = await DbSet.Include(i => i.UserClaims).Where(x => x.Name == oldName).FirstAsync(); entity.Id = savedIr.Id; entity.ShallowCopyTo(savedIr); }
public async Task CreateIdentityResource(string siteId, IdentityResource identityResource, CancellationToken cancellationToken = default(CancellationToken)) { var ent = identityResource.ToEntity(); ent.SiteId = siteId; context.IdentityResources.Add(ent); await context.SaveChangesAsync(); }
public async Task AddAsync(IdentityResource identityResource) { var identityResourceEntity = identityResource.ToEntity(); await this._configurationDbContext.IdentityResources.AddAsync(identityResourceEntity); await this._configurationDbContext.SaveChangesAsync(); }
public void CanMapIdentityResources() { var model = new IdentityResource(); var mappedEntity = model.ToEntity(); var mappedModel = mappedEntity.ToModel(); Assert.NotNull(mappedModel); Assert.NotNull(mappedEntity); }
public void IdentityResourceAutomapperConfigurationIsValid() { var model = new IdentityResource(); var mappedEntity = model.ToEntity(); var mappedModel = mappedEntity.ToModel(); Assert.NotNull(mappedModel); Assert.NotNull(mappedEntity); IdentityResourceMappers.Mapper.ConfigurationProvider.AssertConfigurationIsValid(); }
public async Task <int> AddIdentityResourcesAsync(IdentityResource resource) { var result = await this._configContext.IdentityResources.AddAsync(resource.ToEntity()); await this._configContext.SaveChangesAsync(); resource = result.Entity.ToModel(); return(result.Entity.Id); }
public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(DbContextOptions <ConfigurationDbContext> options) { var visibleIdentityResource = CreateIdentityTestResource(); var visibleApiResource = CreateApiResourceTestResource(); var visibleApiScope = CreateApiScopeTestResource(); var hiddenIdentityResource = new IdentityResource { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource { Name = Guid.NewGuid().ToString(), Scopes = { Guid.NewGuid().ToString() }, ShowInDiscoveryDocument = false }; var hiddenApiScope = new ApiScope { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; using (var context = new ConfigurationDbContext(options, StoreOptions)) { context.IdentityResources.Add(visibleIdentityResource.ToEntity()); context.ApiResources.Add(visibleApiResource.ToEntity()); context.ApiScopes.Add(visibleApiScope.ToEntity()); context.IdentityResources.Add(hiddenIdentityResource.ToEntity()); context.ApiResources.Add(hiddenApiResource.ToEntity()); context.ApiScopes.Add(hiddenApiScope.ToEntity()); context.SaveChanges(); } Resources resources; using (var context = new ConfigurationDbContext(options, StoreOptions)) { var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create()); resources = await store.GetAllResourcesAsync(); } Assert.NotNull(resources); Assert.NotEmpty(resources.IdentityResources); Assert.NotEmpty(resources.ApiResources); Assert.NotEmpty(resources.ApiScopes); Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name); Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name); Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name); Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name); }
public async Task StoreAsync(IdentityResource model) { Guard.ForNull(model, nameof(model)); Guard.ForNull(model.Name, nameof(model.Name)); var entity = model.ToEntity(); var response = await _identityResourceGrantCosmosStore.UpsertAsync(entity); if (!response.IsSuccess) { _logger.LogCritical("Could not store IdentityResource"); } }
public void Add(IdentityResource identityResource) { var dbidentityResource = FindIdentityResourcesByName(identityResource.Name); if (dbidentityResource != null) { throw new InvalidOperationException($"you can not add an existed identityResource,Name={dbidentityResource.Name}."); } var entity = identityResource.ToEntity(); using (var con = _options.DbProviderFactory.CreateConnection()) { con.ConnectionString = _options.ConnectionString; con.Open(); using (var t = con.BeginTransaction()) { try { string left = _options.ColumnProtect["left"]; string right = _options.ColumnProtect["right"]; var identityResourceid = con.ExecuteScalar <int>($"insert into IdentityResources ({left}Description{right},DisplayName,Emphasize,Enabled,{left}Name{right},Required,ShowInDiscoveryDocument) values (@Description,@DisplayName,@Emphasize,@Enabled,@Name,@Required,@ShowInDiscoveryDocument);{_options.GetLastInsertID}", new { entity.Description, entity.DisplayName, entity.Emphasize, entity.Enabled, entity.Name, entity.Required, entity.ShowInDiscoveryDocument }, commandTimeout: _options.CommandTimeOut, commandType: CommandType.Text, transaction: t); entity.Id = identityResourceid; if (entity.UserClaims != null && entity.UserClaims.Count() > 0) { foreach (var item in entity.UserClaims) { InsertApiResourceClaim(item, entity.Id, con, t); } } t.Commit(); } catch (Exception ex) { t.Rollback(); throw ex; } } } }
public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden() { var visibleIdentityResource = CreateIdentityTestResource(); var visibleApiResource = CreateApiResourceTestResource(); var visibleApiScope = CreateApiScopeTestResource(); var hiddenIdentityResource = new IdentityResource { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource { Name = Guid.NewGuid().ToString(), Scopes = { Guid.NewGuid().ToString() }, ShowInDiscoveryDocument = false }; var hiddenApiScope = new ApiScope { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; await _context.IdentityResources.AddAsync(visibleIdentityResource.ToEntity()); await _context.ApiResources.AddAsync(visibleApiResource.ToEntity()); await _context.ApiScopes.AddAsync(visibleApiScope.ToEntity()); await _context.IdentityResources.AddAsync(hiddenIdentityResource.ToEntity()); await _context.ApiResources.AddAsync(hiddenApiResource.ToEntity()); await _context.ApiScopes.AddAsync(hiddenApiScope.ToEntity()); Resources resources; var store = new ResourceStore(_context, FakeLogger <ResourceStore> .Create()); resources = await store.GetAllResourcesAsync(); Assert.NotNull(resources); Assert.NotEmpty(resources.IdentityResources); Assert.NotEmpty(resources.ApiResources); Assert.NotEmpty(resources.ApiScopes); Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name); Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name); Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name); Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name); }
public static void Tests() => Describe(nameof(IdentityResourceMappers), () => { It("IdentityResource Automapper Configuration is valid", () => IdentityResourceMappers.Mapper.ConfigurationProvider.AssertConfigurationIsValid <IdentityResourceMapperProfile>()); It("Can map IdentityResources", () => { using var session = new Session(); var model = new IdentityResource(); var mappedEntity = model.ToEntity(session); var mappedModel = mappedEntity.ToModel(); mappedModel.Should().NotBeNull(); mappedEntity.Should().NotBeNull(); }); });
public void UpdateClaims(IdentityResource identityResource) { var dbitem = GetByName(identityResource.Name); if (dbitem == null) { throw new InvalidOperationException($"could not update IdentityResources for {identityResource.Name} which not exists."); } var entity = identityResource.ToEntity(); entity.Id = dbitem.Id; using (var con = _options.DbProviderFactory.CreateConnection()) { con.ConnectionString = _options.ConnectionString; UpdateClaims(entity.UserClaims, entity.Id, con, null); } }
public void GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden(DbContextOptions <ConfigurationDbContext> options) { var visibleIdentityResource = CreateIdentityTestResource(); var visibleApiResource = CreateApiTestResource(); var hiddenIdentityResource = new IdentityResource { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource { Name = Guid.NewGuid().ToString(), Scopes = new List <Scope> { new Scope { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false } } }; using (var context = new ConfigurationDbContext(options, StoreOptions)) { context.IdentityResources.Add(visibleIdentityResource.ToEntity()); context.ApiResources.Add(visibleApiResource.ToEntity()); context.IdentityResources.Add(hiddenIdentityResource.ToEntity()); context.ApiResources.Add(hiddenApiResource.ToEntity()); context.SaveChanges(); } Resources resources; using (var context = new ConfigurationDbContext(options, StoreOptions)) { var store = new ResourceStore(context, FakeLogger <ResourceStore> .Create()); resources = store.GetAllResourcesAsync().Result; } Assert.NotNull(resources); Assert.NotEmpty(resources.IdentityResources); Assert.NotEmpty(resources.ApiResources); Assert.True(resources.IdentityResources.Any(x => !x.ShowInDiscoveryDocument)); Assert.True(resources.ApiResources.Any(x => !x.Scopes.Any(y => y.ShowInDiscoveryDocument))); }
public void Update(IdentityResource identityResource) { var dbitem = GetByName(identityResource.Name); if (dbitem == null) { throw new InvalidOperationException($"could not update IdentityResources for {identityResource.Name} which not exists."); } var entity = identityResource.ToEntity(); entity.Id = dbitem.Id; using (var con = _options.DbProviderFactory.CreateConnection()) { con.ConnectionString = _options.ConnectionString; con.Open(); using (var t = con.BeginTransaction()) { try { var ret = con.Execute($"update IdentityResources set {left}Description{right} = @Description," + $"DisplayName=@DisplayName," + $"Enabled=@Enabled," + $"Emphasize=@Emphasize," + $"{left}Name{right}=@Name," + $"Required=@Required," + $"ShowInDiscoveryDocument=@ShowInDiscoveryDocument where Id=@Id;", entity, commandTimeout: _options.CommandTimeOut, commandType: CommandType.Text, transaction: t); UpdateClaims(entity.UserClaims, entity.Id, con, t); t.Commit(); } catch (Exception ex) { t.Rollback(); throw ex; } } } }
public async Task <String> CreateIdentityResource(String name, String displayName, String description, Boolean required, Boolean emphasize, Boolean showInDiscoveryDocument, List <String> claims, CancellationToken cancellationToken) { IdentityResource identityResource = new IdentityResource(name, displayName, claims); identityResource.Emphasize = emphasize; identityResource.Required = required; identityResource.ShowInDiscoveryDocument = showInDiscoveryDocument; identityResource.Description = description; // Now translate the model to the entity await this.ConfigurationDbContext.IdentityResources.AddAsync(identityResource.ToEntity(), cancellationToken); // Save the changes await this.ConfigurationDbContext.SaveChangesAsync(); return(name); }
public async Task Should_Retrieve_All_Resources_Including_Hidden_Ones(TestDatabase testDb) { var visibleIdentityResource = CreateTestIdentityResource("identity_visible"); var visibleApiResource = CreateTestApiResource("api_visible", new[] { Guid.NewGuid().ToString() }); var visibleApiScope = CreateTestApiScopeForResource("api_visible_scope", new[] { Guid.NewGuid().ToString() }); visibleApiResource.Scopes.Add(visibleApiScope.Name); var hiddenIdentityResource = new IdentityResource() { Name = "identity_hidden", ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource() { Name = "api_hidden", Scopes = { Guid.NewGuid().ToString() }, ShowInDiscoveryDocument = false }; var hiddenApiScope = new ApiScope() { Name = "api_scope_hidden", ShowInDiscoveryDocument = false }; using (var session = testDb.OpenSession()) { await session.SaveAsync(visibleIdentityResource.ToEntity()); await session.SaveAsync(visibleApiResource.ToEntity()); await session.SaveAsync(visibleApiScope.ToEntity()); await session.SaveAsync(hiddenIdentityResource.ToEntity()); await session.SaveAsync(hiddenApiResource.ToEntity()); await session.SaveAsync(hiddenApiScope.ToEntity()); await session.FlushAsync(); } var loggerMock = new Mock <ILogger <ResourceStore> >(); Resources resources; using (var session = testDb.OpenStatelessSession()) { var store = new ResourceStore(session, loggerMock.Object); resources = await store.GetAllResourcesAsync(); } resources.Should().NotBeNull(); resources.IdentityResources.Should().NotBeEmpty(); resources.IdentityResources.Count.Should().Be(2); resources.ApiResources.Should().NotBeEmpty(); resources.ApiResources.Count.Should().Be(2); resources.ApiScopes.Should().NotBeEmpty(); resources.ApiScopes.Count.Should().Be(2); resources.IdentityResources.Any(ir => ir.Name == visibleIdentityResource.Name).Should().BeTrue(); resources.IdentityResources.Any(ir => ir.Name == hiddenIdentityResource.Name).Should().BeTrue(); resources.ApiResources.Any(ar => ar.Name == visibleApiResource.Name).Should().BeTrue(); resources.ApiResources.Any(ar => ar.Name == hiddenApiResource.Name).Should().BeTrue(); resources.ApiScopes.Any(s => s.Name == visibleApiScope.Name).Should().BeTrue(); resources.ApiScopes.Any(s => s.Name == hiddenApiScope.Name).Should().BeTrue(); await CleanupTestDataAsync(testDb); }
public IActionResult IdentityResource(IdentityResource identityResource) { _configurationDbContext.IdentityResources.Add(identityResource.ToEntity()); _configurationDbContext.SaveChanges(); return(View()); }
public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden() { var visibleIdentityResource = CreateIdentityTestResource(); var visibleApiResource = CreateApiResourceTestResource(); var visibleApiScope = CreateApiScopeTestResource(); var hiddenIdentityResource = new IdentityResource { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource { Name = Guid.NewGuid().ToString(), Scopes = { Guid.NewGuid().ToString() }, ShowInDiscoveryDocument = false }; var hiddenApiScope = new ApiScope { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var identityResourcesRepo = g.configurationDb.GetRepository <Storage.Entities.IdentityResource>(); var apiResourcesRepo = g.configurationDb.GetRepository <Storage.Entities.ApiResource>(); var apiScopesRepo = g.configurationDb.GetRepository <Storage.Entities.ApiScope>(); var visibleIdentityResourceEntity = visibleIdentityResource.ToEntity(); identityResourcesRepo.Insert(visibleIdentityResourceEntity); identityResourcesRepo.SaveMany(visibleIdentityResourceEntity, "UserClaims"); identityResourcesRepo.SaveMany(visibleIdentityResourceEntity, "Properties"); var visibleApiResourceEntity = visibleApiResource.ToEntity(); apiResourcesRepo.Insert(visibleApiResourceEntity); apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Secrets"); apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Scopes"); apiResourcesRepo.SaveMany(visibleApiResourceEntity, "UserClaims"); apiResourcesRepo.SaveMany(visibleApiResourceEntity, "Properties"); var visibleApiScopeEntity = visibleApiScope.ToEntity(); apiScopesRepo.Insert(visibleApiScopeEntity); apiScopesRepo.SaveMany(visibleApiScopeEntity, "UserClaims"); apiScopesRepo.SaveMany(visibleApiScopeEntity, "Properties"); var hiddenIdentityResourceEntity = hiddenIdentityResource.ToEntity(); identityResourcesRepo.Insert(hiddenIdentityResourceEntity); identityResourcesRepo.SaveMany(hiddenIdentityResourceEntity, "UserClaims"); identityResourcesRepo.SaveMany(hiddenIdentityResourceEntity, "Properties"); var hiddenApiResourceEntity = hiddenApiResource.ToEntity(); apiResourcesRepo.Insert(hiddenApiResourceEntity); apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Secrets"); apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Scopes"); apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "UserClaims"); apiResourcesRepo.SaveMany(hiddenApiResourceEntity, "Properties"); var hiddenApiScopeEntity = hiddenApiScope.ToEntity(); apiScopesRepo.Insert(hiddenApiScopeEntity); apiScopesRepo.SaveMany(hiddenApiScopeEntity, "UserClaims"); apiScopesRepo.SaveMany(hiddenApiScopeEntity, "Properties"); Resources resources; var store = new ResourceStore(g.configurationDb, FakeLogger <ResourceStore> .Create()); resources = await store.GetAllResourcesAsync(); Assert.NotNull(resources); Assert.NotEmpty(resources.IdentityResources); Assert.NotEmpty(resources.ApiResources); Assert.NotEmpty(resources.ApiScopes); Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name); Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name); Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name); Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name); }
public async Task AddIdentityResource(IdentityResource entity) { await _configurationDbContext.IdentityResources.AddAsync(entity.ToEntity()); }
public async Task GetAllResources_WhenAllResourcesRequested_ExpectAllResourcesIncludingHidden() { using var ravenStore = GetDocumentStore(); await new ApiResourceIndex().ExecuteAsync(ravenStore); var visibleIdentityResource = CreateIdentityTestResource(); var visibleApiResource = CreateApiResourceTestResource(); var visibleApiScope = CreateApiScopeTestResource(); var hiddenIdentityResource = new IdentityResource { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; var hiddenApiResource = new ApiResource { Name = Guid.NewGuid().ToString(), Scopes = { Guid.NewGuid().ToString() }, ShowInDiscoveryDocument = false }; var hiddenApiScope = new ApiScope { Name = Guid.NewGuid().ToString(), ShowInDiscoveryDocument = false }; using (var session = ravenStore.OpenSession()) { session.Store(visibleIdentityResource.ToEntity()); session.Store(visibleApiResource.ToEntity()); session.Store(visibleApiScope.ToEntity()); session.Store(hiddenIdentityResource.ToEntity()); session.Store(hiddenApiResource.ToEntity()); session.Store(hiddenApiScope.ToEntity()); session.SaveChanges(); } WaitForIndexing(ravenStore); WaitForUserToContinueTheTest(ravenStore); Resources resources; using (var session = ravenStore.OpenAsyncSession()) { var store = new ResourceStore(session, FakeLogger <ResourceStore> .Create()); resources = await store.GetAllResourcesAsync(); } Assert.NotNull(resources); Assert.NotEmpty(resources.IdentityResources); Assert.NotEmpty(resources.ApiResources); Assert.NotEmpty(resources.ApiScopes); Assert.Contains(resources.IdentityResources, x => x.Name == visibleIdentityResource.Name); Assert.Contains(resources.IdentityResources, x => x.Name == hiddenIdentityResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == visibleApiResource.Name); Assert.Contains(resources.ApiResources, x => x.Name == hiddenApiResource.Name); Assert.Contains(resources.ApiScopes, x => x.Name == visibleApiScope.Name); Assert.Contains(resources.ApiScopes, x => x.Name == hiddenApiScope.Name); }
public static void PopulateDatabase(IApplicationBuilder app) { using var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope(); using var configurationContext = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>(); //Seed clients bool frontEndClientExists = configurationContext.Clients.Any(x => x.ClientId == "7ceea8f0-9ef6-4a41-b0d7-d4ebe99430bb"); if (!frontEndClientExists) { Client frontEndClient = new Client { ClientName = "Microservices.WebUI", ClientId = "7ceea8f0-9ef6-4a41-b0d7-d4ebe99430bb", AllowedGrantTypes = GrantTypes.Implicit, RequireConsent = false, AllowAccessTokensViaBrowser = true, AlwaysIncludeUserClaimsInIdToken = true, PostLogoutRedirectUris = new List <string>() { "http://localhost:4200" }, RedirectUris = new List <string>() { "http://localhost:4200/authentication/signin-oidc", "http://localhost:4200/authentication/redirect-silent" }, AllowedScopes = new List <string>() { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile, "roles", "portal-gateway", "product-service-api" }, ClientSecrets = new List <Secret>() { new Secret("2af62f010fab4e558324b841d8006be1".Sha256()) }, Claims = new List <ClientClaim>() { new ClientClaim(string.Empty, "openid"), new ClientClaim(string.Empty, "profile"), new ClientClaim(string.Empty, "roles") } }; configurationContext.Clients.Add(frontEndClient.ToEntity()); } //Seed identity resources IdentityResources.OpenId openId = new IdentityResources.OpenId(); if (!configurationContext.IdentityResources.Any(x => x.Name == openId.Name)) { configurationContext.IdentityResources.Add(openId.ToEntity()); } IdentityResources.Profile profile = new IdentityResources.Profile(); if (!configurationContext.IdentityResources.Any(x => x.Name == profile.Name)) { configurationContext.IdentityResources.Add(profile.ToEntity()); } IdentityResource role = new IdentityResource("roles", "Your role(s)", new[] { "role" }); if (!configurationContext.IdentityResources.Any(x => x.Name == role.Name)) { configurationContext.IdentityResources.Add(role.ToEntity()); } //Seed api resources ApiResource portalGateway = new ApiResource("portal-gateway", "Portal.Gateway", new[] { "role" }); if (!configurationContext.ApiResources.Any(x => x.Name == portalGateway.Name)) { configurationContext.ApiResources.Add(portalGateway.ToEntity()); } ApiResource productService = new ApiResource("product-service-api", "ProductService.API", new[] { "role" }); if (!configurationContext.ApiResources.Any(x => x.Name == productService.Name)) { configurationContext.ApiResources.Add(productService.ToEntity()); } ApiResource pricingService = new ApiResource("pricing-service-api", "PricingService.API", new[] { "role" }); if (!configurationContext.ApiResources.Any(x => x.Name == pricingService.Name)) { configurationContext.ApiResources.Add(pricingService.ToEntity()); } ApiResource policyService = new ApiResource("policy-service-api", "PolicyService.API", new[] { "role" }); if (!configurationContext.ApiResources.Any(x => x.Name == policyService.Name)) { configurationContext.ApiResources.Add(policyService.ToEntity()); } ApiResource policySearchService = new ApiResource("policy-search-service-api", "PolicySearchService.API", new[] { "role" }); if (!configurationContext.ApiResources.Any(x => x.Name == policySearchService.Name)) { configurationContext.ApiResources.Add(policySearchService.ToEntity()); } ApiResource paymentService = new ApiResource("payment-service-api", "PaymentService.API", new[] { "role" }); if (!configurationContext.ApiResources.Any(x => x.Name == paymentService.Name)) { configurationContext.ApiResources.Add(paymentService.ToEntity()); } //.AddInMemoryApiScopes(new IdentityServer4.Models.ApiScope[] { new IdentityServer4.Models.ApiScope("portal-gateway", "Portal.Gateway", new[] { "role" })}) ApiScope portalGatewayScope = new ApiScope("portal-gateway", "Portal.Gateway", new[] { "role" }); if (!configurationContext.ApiScopes.Any(x => x.Name == portalGatewayScope.Name)) { configurationContext.ApiScopes.Add(portalGatewayScope.ToEntity()); } configurationContext.SaveChanges(); }
public void Add(IdentityResource obj) { DbSet.Add(obj.ToEntity()); }
public async Task AddIdentityResourceAsync(IdentityResource resource) { await _configurationDbContext.AddIdentityResource(resource.ToEntity()); }