Guest User

Untitled

a guest
Oct 3rd, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. namespace Jobs
  2. {
  3. public class Startup
  4. {
  5. IConfigurationRoot Configuration;
  6.  
  7. public Startup(IHostingEnvironment env)
  8. {
  9. Configuration = new ConfigurationBuilder()
  10. .SetBasePath(env.ContentRootPath)
  11. .AddJsonFile("appsettings.json").Build();
  12. }
  13.  
  14. // This method gets called by the runtime. Use this method to add services to the container.
  15. public void ConfigureServices(IServiceCollection services)
  16. {
  17. services.Configure<RazorViewEngineOptions>(opt =>
  18. {
  19. opt.ViewLocationExpanders.Add(new RazorViewExpander());
  20. });
  21.  
  22. services.Configure<CookiePolicyOptions>(options =>
  23. {
  24. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  25. options.CheckConsentNeeded = context => true;
  26. options.MinimumSameSitePolicy = SameSiteMode.None;
  27. });
  28.  
  29. services.ConfigureApplicationCookie(options => { options.LoginPath = "/Account/Login"; });
  30.  
  31. services.AddDbContext<JobsStoreDbContext>(options =>{
  32. options.UseSqlServer(Configuration["Data:JobsStore:ConnectionString"]);
  33. });
  34.  
  35. // Add Identity services to the services container
  36. services.AddIdentity<AppUser, IdentityRole>()
  37. .AddEntityFrameworkStores<JobsStoreDbContext>()
  38. .AddDefaultTokenProviders();
  39.  
  40. services.AddSingleton<IJobsRepository, EFJobsRepository>();
  41.  
  42. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
  43. services.AddMemoryCache();
  44. }
  45.  
  46. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  47. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  48. {
  49. if (env.IsDevelopment())
  50. {
  51. app.UseDeveloperExceptionPage();
  52. app.UseAuthentication();
  53. }
  54. else
  55. {
  56. app.UseExceptionHandler("/Home/Error");
  57. }
  58.  
  59. app.UseStaticFiles();
  60. app.UseCookiePolicy();
  61. JobsStoreDbContext.CreateAdminAccount(app.ApplicationServices, Configuration).Wait();
  62. app.UseMvc(routes =>
  63. {
  64. routes.MapRoute(
  65. name: "default",
  66. template: "{controller=Home}/{action=Index}/{id?}");
  67. });
  68.  
  69. }
  70. }
  71.  
  72. public static async Task CreateAdminAccount(IServiceProvider serviceProvider,
  73. IConfiguration configuration)
  74. {
  75. UserManager<AppUser> userManager = serviceProvider
  76. .GetRequiredService<UserManager<AppUser>>();
  77. RoleManager<IdentityRole> roleManager = serviceProvider
  78. .GetRequiredService<RoleManager<IdentityRole>>();
  79.  
  80. string username = configuration["Data:AdminAccount:Name"];
  81. string email = configuration["Data:AdminAccount:Email"];
  82. string password = configuration["Data:AdminAccount:Password"];
  83. string role = configuration["Data:AdminAccount:Role"];
  84.  
  85. if (await userManager.FindByNameAsync(username) == null)
  86. {
  87. if (await roleManager.FindByNameAsync(role) == null)
  88. {
  89. await roleManager.CreateAsync(new IdentityRole(role));
  90. }
  91. AppUser user = new AppUser()
  92. {
  93. UserName = username,
  94. Email = email
  95. };
  96.  
  97. IdentityResult result =
  98. await userManager.CreateAsync(user, password);
  99.  
  100. if (result.Succeeded)
  101. {
  102. await userManager.AddToRoleAsync(user, role);
  103. }
  104. }
  105. }
  106.  
  107. An error occurred while starting the application.
Add Comment
Please, Sign In to add comment