public async static Task InitizeIdentityserverDatabase(PersistedGrantDbContext persistedGrantDbContext, ConfigurationDbContext configurationDbContext)
 {
     if (!configurationDbContext.Clients.Any())
     {
         foreach (var clients in Config.GetClients())
         {
             await configurationDbContext.Clients.AddAsync(clients.ToEntity());
         }
         configurationDbContext.SaveChangesAsync().GetAwaiter().GetResult();
     }
     if (!configurationDbContext.ApiResources.Any())
     {
         foreach (var apiResource in Config.GetAllApiResources())
         {
             await configurationDbContext.ApiResources.AddAsync(apiResource.ToEntity());
         }
         configurationDbContext.SaveChangesAsync().GetAwaiter().GetResult();
     }
     if (!configurationDbContext.IdentityResources.Any())
     {
         foreach (var identityResource in Config.GetAllApiIdentityResources())
         {
             await configurationDbContext.IdentityResources.AddAsync(identityResource.ToEntity());
         }
         configurationDbContext.SaveChangesAsync().GetAwaiter().GetResult();
     }
 }
Example #2
0
 public Migrator(ApplicationDbContext applicationDbContext, PersistedGrantDbContext persistedGrantDbContext,
                 ConfigurationDbContext configurationDbContext)
 {
     _applicationDbContext    = applicationDbContext;
     _persistedGrantDbContext = persistedGrantDbContext;
     _configurationDbContext  = configurationDbContext;
 }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ConfigurationDbContext configurationDbContext, PersistedGrantDbContext persistedGrantDbContext, LvMiniDbContext lvMiniDbContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            configurationDbContext.Database.Migrate();
            configurationDbContext.SeedDataForContext();

            persistedGrantDbContext.Database.Migrate();

            lvMiniDbContext.SeedUsersForContext();

            FileExtensionContentTypeProvider typeProvider = new FileExtensionContentTypeProvider();

            if (!typeProvider.Mappings.ContainsKey(".woff2"))
            {
                typeProvider.Mappings.Add(".woff2", "application/font-woff2");
            }
            app.UseIdentityServer();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }
 public ConfigurationDataInitializer(ConfigurationDbContext configContext, PersistedGrantDbContext prstContext, ConfigCommon config, ApplicationDbContext dbContext)
 {
     this.cfgDbContext  = configContext;
     this.prstDbContext = prstContext;
     this.dbContext     = dbContext;
     this.config        = config;
 }
        public IActionResult Get()
        {
            var options = new DbContextOptions <PersistedGrantDbContext>();

            var store = new OperationalStoreOptions();

            using (var ctx = new PersistedGrantDbContext(options, store))
            {
                ctx.Add <Client>(new Client
                {
                    ClientId      = "c1",
                    ClientSecrets = new List <ClientSecret> {
                        new ClientSecret()
                        {
                            Type = "SharedSecret", Value = "secret"
                        }
                    },
                    AllowedGrantTypes = new List <ClientGrantType> {
                        new ClientGrantType {
                            GrantType = "client_id"
                        }
                    },
                    AllowedScopes = new List <ClientScope> {
                        new ClientScope {
                            Scope = "api1"
                        }
                    }
                });
                ctx.SaveChanges();
            }

            return(Ok());
        }
Example #6
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env,
                              ILoggerFactory logger, UserContext userContext,
                              ConfigurationDbContext configurationDbContext, PersistedGrantDbContext persistedGrantDbContext)
        {
            //var configuration = app.ApplicationServices.GetService<TelemetryConfiguration>();
            //configuration.DisableTelemetry = true;
            logger.AddConsole();
            logger.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            AutoMapper.Mapper.Initialize(cfg =>
            {
                cfg.CreateMap <RegisterUserViewModel, User>()
                .ForMember(d => d.Username, opt => opt.MapFrom(s => s.UserName));
            });

            persistedGrantDbContext.Database.Migrate();

            configurationDbContext.Database.Migrate();
            configurationDbContext.EnsureSeedDataForContext();

            userContext.Database.Migrate();
            userContext.EnsureSeedDataForContext();


            app.UseIdentityServer();

            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }
Example #7
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                              MarvinUserContext marvinUserContext, ConfigurationDbContext configurationDbContext,
                              PersistedGrantDbContext persistedGrantDbContext)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                loggerFactory.AddDebug();
                app.UseDeveloperExceptionPage();
            }
            else
            {
                persistedGrantDbContext.Database.Migrate();

                configurationDbContext.Database.Migrate();
                configurationDbContext.EnsureSeedDataForContext();

                marvinUserContext.Database.Migrate();
                marvinUserContext.EnsureSeedDataForContext();
            }

            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseStaticFiles();
            app.UseMvcWithDefaultRoute();
        }
Example #8
0
        public void Configure
        (
            IApplicationBuilder application,
            ApplicationIdentityDbContext identityContext,
            ConfigurationDbContext configurationContext,
            PersistedGrantDbContext persistedGrantContext,
            UserManager <ApplicationIdentityUser> userManager
        )
        {
            application.UseCors(cors => cors.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
            application.UseHsts();
            application.UseHttpsRedirection();
            application.UseStaticFiles();
            application.UseRouting();
            application.UseAuthentication();
            application.UseAuthorization();
            application.UseEndpoints(builder => builder.MapDefaultControllerRoute());
            application.UseIdentityServer();

            identityContext.Database.Migrate();
            configurationContext.Database.Migrate();
            persistedGrantContext.Database.Migrate();

            configurationContext.Seed();
            userManager.Seed();
        }
Example #9
0
 public void InitIdentityServer4Database(IApplicationBuilder app)
 {
     using (var scope = app.ApplicationServices.CreateScope())
     {
         ConfigurationDbContext  configurationDbContext  = scope.ServiceProvider.GetRequiredService <ConfigurationDbContext>();
         PersistedGrantDbContext persistedGrantDbContext = scope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>();
         if (!configurationDbContext.Clients.Any())
         {
             foreach (var client in Config.GetClient())
             {
                 configurationDbContext.Clients.Add(client.ToEntity());
             }
             configurationDbContext.SaveChanges();
         }
         if (!configurationDbContext.ApiResources.Any())
         {
             foreach (var client in Config.GetResource())
             {
                 configurationDbContext.ApiResources.Add(client.ToEntity());
             }
             configurationDbContext.SaveChanges();
         }
         if (!configurationDbContext.IdentityResources.Any())
         {
             foreach (var client in Config.GetIdentityResource())
             {
                 configurationDbContext.IdentityResources.Add(client.ToEntity());
             }
             configurationDbContext.SaveChanges();
         }
     }
 }
        /// <summary>
        /// Initialises the persisted grant database.
        /// </summary>
        /// <param name="persistedGrantDbContext">The persisted grant database context.</param>
        /// <param name="seedingType">Type of the seeding.</param>
        public static void InitialisePersistedGrantDatabase(PersistedGrantDbContext persistedGrantDbContext,
                                                            SeedingType seedingType)
        {
            Boolean isDbInitialised = false;
            Int32   retryCounter    = 0;

            while (retryCounter < 20 && !isDbInitialised)
            {
                try
                {
                    if (persistedGrantDbContext.Database.IsSqlServer())
                    {
                        persistedGrantDbContext.Database.Migrate();
                    }

                    persistedGrantDbContext.SaveChanges();

                    isDbInitialised = true;
                    break;
                }
                catch (Exception ex)
                {
                    retryCounter++;
                    Thread.Sleep(10000);
                }
            }

            if (!isDbInitialised)
            {
                String connString = persistedGrantDbContext.Database.GetDbConnection().ConnectionString;

                Exception newException = new Exception($"Error initialising Db with Connection String [{connString}]");
                throw newException;
            }
        }
Example #11
0
        public void Configure
        (
            IApplicationBuilder application,
            ApplicationDbContext applicationDbContext,
            ConfigurationDbContext configurationDbContext,
            PersistedGrantDbContext persistedGrantDbContext,
            UserManager <IdentityUser> userManager
        )
        {
            applicationDbContext.Database.Migrate();
            configurationDbContext.Database.Migrate();
            persistedGrantDbContext.Database.Migrate();

            configurationDbContext.Seed();
            userManager.Seed();

            application.UseException();
            application.UseCookiePolicy();
            application.UseCorsAllowAny();
            application.UseHsts();
            application.UseHttpsRedirection();
            application.UseStaticFiles();
            application.UseRouting();
            application.UseIdentityServer();
            application.UseAuthentication();
            application.UseAuthorization();
            application.UseEndpoints();
        }
Example #12
0
 public DbInitializer(PersistedGrantDbContext persistedGrantDbContext, ConfigurationDbContext configurationDbContext, ApplicationDbContext applicationDbContext, UserManager <ApplicationUser> userManager)
 {
     this._persistedGrantDbContext = persistedGrantDbContext;
     this._configurationDbContext  = configurationDbContext;
     this._applicationDbContext    = applicationDbContext;
     this._userManager             = userManager;
 }
Example #13
0
        public static async Task EnsureSeedDataAsync(PersistedGrantDbContext grantContext, ConfigurationDbContext configContext)
        {
            grantContext.Database.Migrate();
            configContext.Database.Migrate();

            if (!configContext.Clients.Any())
            {
                foreach (var client in Config.GetClients().ToList())
                {
                    configContext.Clients.Add(client.ToEntity());
                }
            }

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

            if (!configContext.ApiResources.Any())
            {
                foreach (var resource in Config.GetApis().ToList())
                {
                    configContext.ApiResources.Add(resource.ToEntity());
                }
            }

            await configContext.SaveChangesAsync();
        }
Example #14
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
                              MarvinUserContext marvinUserContext,
                              ConfigurationDbContext configurationDbContext,
                              PersistedGrantDbContext persistedGrantDbContext)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            configurationDbContext.Database.Migrate();
            configurationDbContext.EnsureSeedDataForContext();

            marvinUserContext.Database.Migrate();
            marvinUserContext.EnsureSeedDataForContext();

            persistedGrantDbContext.Database.Migrate();

            app.UseStaticFiles();
            app.UseIdentityServer();
            app.UseMvcWithDefaultRoute();

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});
        }
Example #15
0
 public Config(ConfigurationDbContext configurationDbContext, PersistedGrantDbContext persistedGrantDbContext, IConfiguration configuration, ILogger <Config> logger)
 {
     this.configurationDbContext  = configurationDbContext;
     this.persistedGrantDbContext = persistedGrantDbContext;
     this.configuration           = configuration;
     this.logger = logger;
 }
Example #16
0
        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            ILoggerFactory loggerFactory,
            UserContext userContext,
            ConfigurationDbContext configurationDbContext,
            PersistedGrantDbContext persistedGrantDbContext)
        {
            loggerFactory.AddConsole();
            loggerFactory.AddDebug();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // Garantindo que o contexto dos usuarios executara o Migrations
            // e o Seed será executado
            userContext.Database.Migrate();
            userContext.EnsureSeedDataForContext();

            // Garantindo que o contexto de configurações executara o Migrations
            // e o Seed será executado
            configurationDbContext.Database.Migrate();
            configurationDbContext.EnsureSeedDataForContext();

            // Garantindo que o contexto de configurações executara o Migrations
            persistedGrantDbContext.Database.Migrate();

            app.UseStaticFiles();
            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseMvcWithDefaultRoute();
        }
Example #17
0
 public InstallController(PersistedGrantDbContext persistedGrantDbContext, ConfigurationDbContext configurationDbContext, IConfiguration configuration, IWebHostEnvironment environment)
 {
     _persistedGrantDbContext = persistedGrantDbContext;
     _configurationDbContext  = configurationDbContext;
     _configuration           = configuration;
     Environment = environment;
 }
        public InitializeDatabaseService(IServiceScopeFactory serviceScopeFactory)
        {
            var sp = serviceScopeFactory.CreateScope().ServiceProvider;

            _configurationDbContext  = sp.GetRequiredService <ConfigurationDbContext>();
            _persistedGrantDbContext = sp.GetRequiredService <PersistedGrantDbContext>();
            _identityContext         = sp.GetRequiredService <IdentityContext>();
        }
Example #19
0
        public PersistedGrantRepositoryTest()
        {
            var options = new DbContextOptionsBuilder <PersistedGrantDbContext>()
                          .UseInMemoryDatabase("RivaIdentityIdentityServerIntegrationTestsDb").Options;

            _context = new PersistedGrantDbContext(options, new OperationalStoreOptions());
            _persistedGrantRepository = new PersistedGrantRepository(_context);
        }
 public DatabaseInitializer(ILogger <DatabaseInitializer> logger, PersistedGrantDbContext persistedGrantContext, ConfigurationDbContext configurationContext, ApplicationDbContext applicationContext, IAccountManager accountManager)
 {
     _logger = logger;
     _persistedGrantContext = persistedGrantContext;
     _configurationContext  = configurationContext;
     _applicationContext    = applicationContext;
     _accountManager        = accountManager;
 }
Example #21
0
 public AccountDataConsistencyService(RivaIdentityDbContext rivaIdentityDbContext, PersistedGrantDbContext persistedGrantDbContext,
                                      IAccountRepository accountRepository, IPersistedGrantRepository persistedGrantRepository)
 {
     _rivaIdentityDbContext    = rivaIdentityDbContext;
     _persistedGrantDbContext  = persistedGrantDbContext;
     _accountRepository        = accountRepository;
     _persistedGrantRepository = persistedGrantRepository;
 }
Example #22
0
 public SeedDbData(
     PersistedGrantDbContext persistedGrantDbContext,
     ConfigurationDbContext configurationDbContext,
     UserManager <IdentityUser> userManager)
 {
     _persistedGrantDbContext = persistedGrantDbContext;
     _configurationDbContext  = configurationDbContext;
     _userManager             = userManager;
 }
Example #23
0
 public DatabaseInitializer(ILogger <DatabaseInitializer> logger, PersistedGrantDbContext persistedGrantContext, ConfigurationDbContext configurationContext, EventDbContext eventsContext, IdentityDbContext accountsContext, IIdentityManager accountManager)
 {
     _logger = logger;
     _persistedGrantContext = persistedGrantContext;
     _configurationContext  = configurationContext;
     _eventContext          = eventsContext;
     _identityContext       = accountsContext;
     _identityManager       = accountManager;
 }
 public MigrateDatabaseInitialization(
     IdentityDbContext dbContext,
     PersistedGrantDbContext persistedGrantDbContext,
     ConfigurationDbContext configurationDbContext)
 {
     _dbContext = dbContext;
     _persistedGrantDbContext = persistedGrantDbContext;
     _configurationDbContext  = configurationDbContext;
 }
Example #25
0
        static void Main(string[] args)
        {
            var sqlServerConnectionString = "Server=tcp:ntl-kft.database.windows.net,1433;Initial Catalog=Ntl-AuthSSDb;Persist Security Info=False;User ID=ntl-admin;Password=Welcome1@;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";

            var persitantGrantOptionsBuilder = new DbContextOptionsBuilder <PersistedGrantDbContext>();

            persitantGrantOptionsBuilder.UseSqlServer(sqlServerConnectionString, sql => {
                sql.MigrationsHistoryTable("_PersistedGrantMigrations");
            });
            var operationalStoreOptions = new OperationalStoreOptions()
            {
                EnableTokenCleanup = true, TokenCleanupInterval = 30
            };

            using (var persitantGrantDbContext = new PersistedGrantDbContext(persitantGrantOptionsBuilder.Options, operationalStoreOptions))
            {
                //persitantGrantDbContext.Database.Migrate();//No use of this currently as migrations are in different assembly
            }

            var configurationOptionsBuilder = new DbContextOptionsBuilder <ConfigurationDbContext>();

            configurationOptionsBuilder.UseSqlServer(sqlServerConnectionString, sql => {
                sql.MigrationsHistoryTable("_ConfigurationMigrations");
            });

            using (var configurationDbContext = new ConfigurationDbContext(configurationOptionsBuilder.Options, new ConfigurationStoreOptions()))
            {
                //configurationDbContext.Database.Migrate(); //No use of this currently as migrations are in different assembly
                if (!configurationDbContext.Clients.Any())
                {
                    foreach (var client in Config.GetClients())
                    {
                        configurationDbContext.Clients.Add(client.ToEntity());
                    }
                    configurationDbContext.SaveChanges();
                }

                if (!configurationDbContext.ApiResources.Any())
                {
                    foreach (var resource in Config.GetApiResources())
                    {
                        configurationDbContext.ApiResources.Add(resource.ToEntity());
                    }
                    configurationDbContext.SaveChanges();
                }

                if (!configurationDbContext.IdentityResources.Any())
                {
                    foreach (var resource in Config.GetIdentityResources())
                    {
                        configurationDbContext.IdentityResources.Add(resource.ToEntity());
                    }
                    configurationDbContext.SaveChanges();
                }
            }
        }
Example #26
0
 public SeedController(ILogger <SeedController> logger, IConfiguration configuration, UserManager <User> userManager, RoleManager <Role> roleManager, ConfigurationDbContext configurationDbContext, ApplicationDbContext applicationDbContext, PersistedGrantDbContext persistedGrantDbContext)
 {
     _logger                  = logger;
     _configuration           = configuration;
     _userManager             = userManager;
     _roleManager             = roleManager;
     _configurationDbContext  = configurationDbContext;
     _applicationDbContext    = applicationDbContext;
     _persistedGrantDbContext = persistedGrantDbContext;
 }
        public void Can_initialize_PersistedGrantDbContext()
        {
            using var testDatabase = SqlServerTestStore.CreateInitialized("IdentityServerPersistedGrantDbContext");
            var options = CreateOptions(testDatabase);

            using (var context = new PersistedGrantDbContext(options, new OperationalStoreOptions()))
            {
                context.Database.EnsureCreatedResiliently();
            }
        }
 public DbInitializer(ConfigurationDbContext configurationDbContext, PersistedGrantDbContext persistedGrantDbContext,
                      AuthDbContext userDbContext, Config config, UserManager <Account> userManager, RoleManager <IdentityRole> roleManager)
 {
     _config = config;
     _configurationDbContext  = configurationDbContext;
     _persistedGrantDbContext = persistedGrantDbContext;
     _userDbContext           = userDbContext;
     _userManager             = userManager;
     _roleManager             = roleManager;
 }
 public DatabaseInitializer(ApplicationDbContext context, IAccountManager accountManager, ILogger <DatabaseInitializer> logger, PersistedGrantDbContext persistedGrantDbContext,
                            ConfigurationDbContext configurationDbContext, ITimedTaskManager timedTaskManager)
 {
     timedTaskManager.Start();
     _accountManager          = accountManager;
     _context                 = context;
     _logger                  = logger;
     _persistedGrantDbContext = persistedGrantDbContext;
     _configurationDbContext  = configurationDbContext;
 }
Example #30
0
 public IdentityController(ApplicationDbContext context, ConfigurationDbContext contextConfig,PersistedGrantDbContext contextPers,
     UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
 {
     _context = context;
     _contextConfig = contextConfig;
     _contextPers = contextPers;
     _userManager = userManager;
     _signInManager = signInManager;
     
 }