// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseRouting(); app.UseStaticFiles(); // global cors policy app.UseCors(x => x .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); app.UseAuthentication(); app.UseAuthorization(); app.UseMiddleware <JwtMiddleware>(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "FBLA SocialApp API V1"); }); using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { var roleManager = serviceScope.ServiceProvider.GetRequiredService <RoleManager <ApplicationRole> >(); var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >(); // seed the AspNetRoles table var roleSeed = new ApplicationRoleSeed(roleManager); roleSeed.CreateRoles(); // seed the AspNetUsers table var userSeed = new ApplicationUserSeed(userManager); userSeed.CreateAdminUser(); } }
private static async Task SeedRoles(ApplicationDbContext context, RoleManager <ApplicationRole> roleManager) { var roles = ApplicationRoleSeed.Seed(); foreach (var role in roles) { var identityResult = roleManager.CreateAsync(role).Result; if (identityResult.Succeeded) { context.SaveChanges(); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseBrowserLink(); app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { var roleManager = serviceScope.ServiceProvider.GetRequiredService <RoleManager <ApplicationRole> >(); var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >(); // seed the AspNetRoles table var roleSeed = new ApplicationRoleSeed(roleManager); roleSeed.CreateRoles(); // seed the AspNetUsers table var userSeed = new ApplicationUserSeed(userManager); userSeed.CreateAdminUser(); } }
public static async Task InitializeAsync(ApiContext context, UserManager <ApplicationUser> userManager, RoleManager <ApplicationRole> roleManager, IServiceProvider serviceProvider) { Assembly dalAssembly = null; var assemblies = AppDomain.CurrentDomain.GetAssemblies(); //Loops through all the found assemblies and stores the NET.UNICA.EDU.DAL assembly foreach (var assembly in assemblies) { var assemblyName = assembly.GetName(); if (assemblyName.Name == "DAL") { dalAssembly = assembly; break; } } //Checks if the global assembly exists if (dalAssembly == null) { var exception = new ApplicationException($"Assembly not found"); throw exception; } //Declares the dictionaries to store the types of the NET.UNICA.EDU.DAL.Models and NET.UNICA.EDU.DAL.Seeds namespaces and gets all the types from the global assembly Dictionary <string, Type> modelsTypes = new Dictionary <string, Type>(); Dictionary <string, Type> seedsTypes = new Dictionary <string, Type>(); var dalAssemblyTypes = dalAssembly.GetTypes(); //Loops through all the assembly types and stores the type if the namespace is either NET.UNICA.EDU.DAL.Models or NET.UNICA.EDU.DAL.Seeds foreach (Type dalAssemblyType in dalAssemblyTypes) { if ( dalAssemblyType.IsDefined(typeof(CompilerGeneratedAttribute), false) || dalAssemblyType.Namespace == null || dalAssemblyType.Name == "<>c" ) { continue; } if (dalAssemblyType.Namespace == "DAL.Models") { modelsTypes.Add(dalAssemblyType.Name, dalAssemblyType); } else if (dalAssemblyType.Namespace == "DAL.Seeds") { seedsTypes.Add(dalAssemblyType.Name, dalAssemblyType); } } context.Database.EnsureCreated(); /* if (!context.Users.Any()) * { * context.Users.AddRange(ApplicationUserSeed.Seed(context,userManager)); * context.SaveChanges(); * } * * * * var Password = "******"; * foreach(var user in context.Users) * { * // var identityResult = await userManager.CreateAsync(user); * // await userManager.AddPasswordAsync(user, Password); * * user.PasswordHash = userManager.PasswordHasher.HashPassword(user, "Lusho"); * } * context.SaveChanges(); * context.SaveChanges();*/ var users = ApplicationUserSeed.Seed(); foreach (var user in users) { var identityResult = await userManager.CreateAsync(user, "Lusho"); /* * if (identityResult.Succeeded) * { * * identityResult = await userManager.AddPasswordAsync(user, "Lusho"); * }*/ } context.SaveChanges(); if (!context.Roles.Any()) { context.Roles.AddRange(ApplicationRoleSeed.Seed()); context.SaveChanges(); } SeedRoles newSeed = new SeedRoles(roleManager); if (!context.UserRoles.Any()) { context.UserRoles.AddRange(ApplicationUserRoleSeed.Seed(context)); context.SaveChanges(); } /* * var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>(); * string[] roleNames = { "Admin", "Member" }; * IdentityResult roleResult; * foreach(var roleName in roleNames) * { * var roleExist = await RoleManager.RoleExistsAsync(roleName); * if (!roleExist) * { * roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName)); * } * }*/ }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } if (env.IsProduction()) { app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedProto }); } app.UseRouting(); app.UseStaticFiles(); app.UseCookiePolicy(); // https://docs.microsoft.com/en-us/aspnet/core/security/samesite?view=aspnetcore-3.1 app.UseAuthentication(); app.UseAuthorization(); // Set up hangfire capabilities var options = new DashboardOptions { Authorization = new[] { new HangfireAuthorizationFilter() } }; app.UseHangfireDashboard("/hangfire", options); app.UseHangfireServer(); RecurringJob.AddOrUpdate <IGenerateTranscripts>( generator => generator.Execute(), Cron.Daily); BackgroundJob.Enqueue <IStudentDataImporter>( generator => generator.Execute()); //BackgroundJob.Enqueue<IEmailQueue>( // queue => queue.SendMail("*****@*****.**", "Test Message", "this is a scholarship email test.. did you get this?")); //BackgroundJob.Enqueue<ICreateApplicationPackage>(queue => queue.Execute()); // If you want to run the job immediately /* * BackgroundJob.Enqueue<IGenerateTranscripts>( * generator => generator.Execute()); */ // Set up App_Data directory // set var baseDir = env.ContentRootPath; AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(baseDir, "App_Data")); app.UseStatusCodePagesWithRedirects("/error/{0}"); app.UseAnalyticsLogger(); // Custom middleware app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( "default", "{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( "securedownload", "{controller=SecureDownload}/{id?}/{filename?}", new { controller = "SecureDownload", action = "Download" } ); }); using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { var roleManager = serviceScope.ServiceProvider.GetRequiredService <RoleManager <ApplicationRole> >(); var userManager = serviceScope.ServiceProvider.GetRequiredService <UserManager <ApplicationUser> >(); var dbContext = serviceScope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // seed the AspNetRoles table var roleSeed = new ApplicationRoleSeed(roleManager); roleSeed.CreateRoles(); // seed the AspNetUsers table var userSeed = new ApplicationUserSeed(userManager, dbContext); userSeed.CreateAdminUser(); var dbSeed = new ApplicationDbSeed(dbContext); dbSeed.SeedDatabase(); } app.UseSmidge(bundles => { bundles.CreateCss("scholarship-application-css", "~/lib/bootstrap/dist/css/bootstrap.css", "~/lib/font-awesome/css/font-awesome.css", "~/css/animate.css", "~/css/style.css", "~/css/site.css", "~/lib/heart/heart.css"); // Libraries bundles.CreateJs("scholarship-js-libraries", "~/lib/jquery/dist/jquery.js", "~/lib/jquery-ui/jquery-ui.js", "~/lib/Popper/popper.js", "~/lib/bootstrap/dist/js/bootstrap.js" ); // Custom scripts bundles.CreateJs("scholarship-js-custom", "~/js/site.js"); // Inspinia scripts bundles.CreateJs("scholarship-js-inspinia", "~/lib/metisMenu/dist/jquery.metisMenu.js", "~/lib/slimScroll/jquery.slimscroll.js", "~/lib/pace/pace.js", "~/js/script.js"); }); }