private static void EnsureSeedData(IConfigurationDbContext context)
        {
            if (!context.Clients.Any())
            {
                foreach (var client in Config.GetClients().ToList())
                {
                    context.AddClient(client.ToEntity());
                }
            }

            if (!context.IdentityResources.Any())
            {
                foreach (var resource in Config.GetIdentityResources().ToList())
                {
                    context.AddIdentityResource(resource.ToEntity());
                }
            }

            if (!context.ApiResources.Any())
            {
                foreach (var resource in Config.GetApiResources().ToList())
                {
                    context.AddApiResource(resource.ToEntity());
                }
            }
        }
Esempio n. 2
0
        private static void EnsureSeedData(IConfigurationDbContext context)
        {
            foreach (var client in Clients.Get().ToList())
            {
                var dbRecords = context.Clients(client.ClientId).ToList();
                if (dbRecords.Count == 0)
                {
                    context.AddClient(client.ToEntity());
                }
            }

            foreach (var resource in Resources.GetIdentityResources().ToList())
            {
                var dbRecords = context.IdentityResources(resource.Name).ToList();
                if (dbRecords.Count == 0)
                {
                    context.AddIdentityResource(resource.ToEntity());
                }
            }

            foreach (var resource in Resources.GetApiResources().ToList())
            {
                var dbRecords = context.ApiResources(resource.Name).ToList();
                if (dbRecords.Count == 0)
                {
                    context.AddApiResource(resource.ToEntity());
                }
            }
        }
        public void InitializeFiles()
        {
            string contentRootPath = string.Empty;
            var    clientJson      = string.Empty;
            var    apiresourceJson = string.Empty;

            if (_env.IsDevelopment())
            {
                contentRootPath = _env.ContentRootPath;
                clientJson      = File.ReadAllText(contentRootPath + "/Config/clients.json");
                apiresourceJson = File.ReadAllText(contentRootPath + "/Config/apiresources.json");
            }
            else
            {
                contentRootPath = _env.ContentRootPath;
                clientJson      = File.ReadAllText(contentRootPath + "/Config/azureclient.json");
                apiresourceJson = File.ReadAllText(contentRootPath + "/Config/apiresources.json");
            }

            var clients   = JsonConvert.DeserializeObject <IEnumerable <IdentityServer4.MongoDB.Entities.Client> >(clientJson);
            var resources = JsonConvert.DeserializeObject <IEnumerable <IdentityServer4.MongoDB.Entities.ApiResource> >(apiresourceJson);

            bool lAdd = false;
            IList <IdentityServer4.MongoDB.Entities.Client> clientsToAdd = new List <IdentityServer4.MongoDB.Entities.Client>();

            foreach (var client in clients)
            {
                var dbClient = _context.Clients
                               .Where(c => c.ClientId == client.ClientId)
                               //.Include(p => p.Properties)
                               .FirstOrDefault();

                if (dbClient == null)
                {
                    lAdd = true;
                    clientsToAdd.Add(client);
                }
                else
                {
                    if (dbClient.Properties.Count > 0)
                    {
                        int.TryParse(dbClient.Properties.FirstOrDefault(c => c.Key == "version").Value, out int dbVersion);
                        int.TryParse(client.Properties.FirstOrDefault(c => c.Key == "version").Value, out int version);
                        if (dbVersion < version)
                        {
                            _context.RemoveClient(dbClient);
                            //_context.SaveChanges();
                            lAdd = true;
                            clientsToAdd.Add(client);
                        }
                    }
                    else
                    {
                        _context.RemoveClient(dbClient);
                        //_context.SaveChanges();
                        lAdd = true;
                        clientsToAdd.Add(client);
                    }
                }
            }

            foreach (var item in clientsToAdd)
            {
                List <IdentityServer4.MongoDB.Entities.ClientSecret> secrets = new List <IdentityServer4.MongoDB.Entities.ClientSecret>()
                {
                    new IdentityServer4.MongoDB.Entities.ClientSecret {
                        Value = "F621F470-9731-4A25-80EF-67A6F7C5F4B8".Sha256()
                    }
                };
                item.ClientSecrets = secrets;

                /*
                 *
                 * item.AllowedGrantTypes = new List<string>() {
                 *      GrantType.ResourceOwnerPassword,
                 *      GrantType.Implicit,
                 *      GrantType.ClientCredentials
                 * };
                 * item.AllowAccessTokensViaBrowser = true;
                 * item.AlwaysIncludeUserClaimsInIdToken = true;
                 * item.AlwaysSendClientClaims = true;
                 * item.AccessTokenType = (int)AccessTokenType.Jwt;
                 * item.AlwaysIncludeUserClaimsInIdToken = true;
                 * item.RequireConsent = false;
                 * item.RefreshTokenExpiration = 1;// IdentityServer4.Models.TokenExpiration.Absolute;
                 *
                 */
                _context.AddClient(item);
            }
            //_context.SaveChanges();

            if (!_context.IdentityResources.Any())
            {
                foreach (var resource in infusync.identity.Resources.GetIdentityResources())
                {
                    _context.AddIdentityResource(new IdentityServer4.MongoDB.Entities.IdentityResource()
                    {
                        Name = resource.Name
                    });
                }
                //_context.SaveChanges();
            }

            IList <IdentityServer4.MongoDB.Entities.ApiResource> resourcesToAdd = new List <IdentityServer4.MongoDB.Entities.ApiResource>();

            foreach (var resource in resources)
            {
                var dbReource = _context.ApiResources
                                .FirstOrDefault(c => c.Name == resource.Name);

                List <IdentityServer4.MongoDB.Entities.ApiSecret> secrets = new List <IdentityServer4.MongoDB.Entities.ApiSecret>()
                {
                    new IdentityServer4.MongoDB.Entities.ApiSecret {
                        Value = "F621F470-9731-4A25-80EF-67A6F7C5F4B8".Sha256()
                    }
                };

                if (dbReource != null)
                {
                    if (lAdd)
                    {
                        dbReource.Secrets = secrets;
                        _context.RemoveApiResource(dbReource);
                        // _context.SaveChanges();
                        resourcesToAdd.Add(resource);
                    }
                }
                else
                {
                    resource.Secrets = secrets;
                    resourcesToAdd.Add(resource);
                }
            }

            foreach (var item in resourcesToAdd)
            {
                _context.AddApiResource(item);
            }
            //_context.SaveChanges();

            if (!_context.Companies.Any())
            {
                _context.AddCompany
                (
                    new Company()
                {
                    ContactEmail     = "*****@*****.**",
                    CompanyName      = "Individual",
                    ContactFirstName = "",
                    ContactLastName  = "",
                    ContactPhone     = "",
                    Id = new MongoDB.Bson.ObjectId("5bc22641aff0d92d64f1aba6")
                }
                );
                //_context.SaveChanges();
            }
        }
 public async Task AddClientAsync(Client client)
 {
     await _configurationDbContext.AddClient(client.ToEntity());
 }