Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Jobs
- {
- public class Startup
- {
- IConfigurationRoot Configuration;
- public Startup(IHostingEnvironment env)
- {
- Configuration = new ConfigurationBuilder()
- .SetBasePath(env.ContentRootPath)
- .AddJsonFile("appsettings.json").Build();
- }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.Configure<RazorViewEngineOptions>(opt =>
- {
- opt.ViewLocationExpanders.Add(new RazorViewExpander());
- });
- services.Configure<CookiePolicyOptions>(options =>
- {
- // This lambda determines whether user consent for non-essential cookies is needed for a given request.
- options.CheckConsentNeeded = context => true;
- options.MinimumSameSitePolicy = SameSiteMode.None;
- });
- services.ConfigureApplicationCookie(options => { options.LoginPath = "/Account/Login"; });
- services.AddDbContext<JobsStoreDbContext>(options =>{
- options.UseSqlServer(Configuration["Data:JobsStore:ConnectionString"]);
- });
- // Add Identity services to the services container
- services.AddIdentity<AppUser, IdentityRole>()
- .AddEntityFrameworkStores<JobsStoreDbContext>()
- .AddDefaultTokenProviders();
- services.AddSingleton<IJobsRepository, EFJobsRepository>();
- services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- services.AddMemoryCache();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseAuthentication();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles();
- app.UseCookiePolicy();
- JobsStoreDbContext.CreateAdminAccount(app.ApplicationServices, Configuration).Wait();
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- }
- }
- public static async Task CreateAdminAccount(IServiceProvider serviceProvider,
- IConfiguration configuration)
- {
- UserManager<AppUser> userManager = serviceProvider
- .GetRequiredService<UserManager<AppUser>>();
- RoleManager<IdentityRole> roleManager = serviceProvider
- .GetRequiredService<RoleManager<IdentityRole>>();
- string username = configuration["Data:AdminAccount:Name"];
- string email = configuration["Data:AdminAccount:Email"];
- string password = configuration["Data:AdminAccount:Password"];
- string role = configuration["Data:AdminAccount:Role"];
- if (await userManager.FindByNameAsync(username) == null)
- {
- if (await roleManager.FindByNameAsync(role) == null)
- {
- await roleManager.CreateAsync(new IdentityRole(role));
- }
- AppUser user = new AppUser()
- {
- UserName = username,
- Email = email
- };
- IdentityResult result =
- await userManager.CreateAsync(user, password);
- if (result.Succeeded)
- {
- await userManager.AddToRoleAsync(user, role);
- }
- }
- }
- An error occurred while starting the application.
Add Comment
Please, Sign In to add comment