Example #1
0
 public ImageService(IOptions <ImageOptions> fileOptions, UserManager <User> userManager, ClaimsPrincipal user, ImageStoreDbContext dbContext)
 {
     _imageOptions = fileOptions.Value;
     _userManager  = userManager;
     _user         = user;
     _dbContext    = dbContext;
 }
Example #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ImageStoreDbContext db)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            db.Database.EnsureCreated();

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
Example #3
0
 public AccountService(UserManager <User> userManager, RoleManager <IdentityRole <int> > roleManager, SignInManager <User> signInManager, ImageStoreDbContext dbContext, IMapper mapper)
 {
     _userManager   = userManager;
     _roleManager   = roleManager;
     _signInManager = signInManager;
     _dbContext     = dbContext;
     _mapper        = mapper;
 }
Example #4
0
        public static async Task Seed(ImageStoreDbContext context, IServiceProvider serviceProvider)
        {
            var dbSeedOptions = serviceProvider.GetRequiredService <IOptions <DbSeedOptions> >().Value;
            var userManager   = serviceProvider.GetRequiredService <UserManager <User> >();
            var roleManager   = serviceProvider.GetRequiredService <RoleManager <IdentityRole <int> > >();

            await CreateRolesAsync(roleManager);

            if (dbSeedOptions.CreateDefaultAdmin)
            {
                var defaultAdmin = new User
                {
                    UserName = dbSeedOptions.DefaultAdminUserName,
                    Email    = dbSeedOptions.DefaultAdminEmailAddress
                };
                await CreateDefaultAdminAsync(userManager, defaultAdmin, dbSeedOptions.DefaultAdminPassword);
            }
        }
Example #5
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ImageStoreDbContext context, IServiceProvider serviceProvider, IOptions <Common.Options.ImageOptions> imageOptions)
        {
            app.UseProblemDetails();

            if (env.IsDevelopment())
            {
                app.UseOpenApi();
                app.UseSwaggerUi3();
            }

            app.UseHttpsRedirection();

            app.UseStaticFiles();

            if (!env.IsDevelopment() || env.ShouldRunAngular())
            {
                app.UseSpaStaticFiles();
            }

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            var filesPath      = Path.Combine(imageOptions.Value.RootPath, imageOptions.Value.FilesPath);
            var thumbnailsPath = Path.Combine(imageOptions.Value.RootPath, imageOptions.Value.ThumbnailsPath);

            Directory.CreateDirectory(filesPath);
            Directory.CreateDirectory(thumbnailsPath);

            app.UseStaticFiles(new StaticFileOptions
            {
                OnPrepareResponse = context =>
                {
                    if (!context.Context.User.Identity.IsAuthenticated)
                    {
                        context.Context.Response.Redirect("/");
                    }
                },
                FileProvider          = new PhysicalFileProvider(filesPath),
                RequestPath           = new PathString("/images"),
                ServeUnknownFileTypes = true
            });

            app.UseStaticFiles(new StaticFileOptions
            {
                OnPrepareResponse = context =>
                {
                    if (!context.Context.User.Identity.IsAuthenticated)
                    {
                        context.Context.Response.Redirect("/");
                    }
                },
                FileProvider = new PhysicalFileProvider(thumbnailsPath),
                RequestPath  = new PathString("/thumbnails")
            });

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            app.UseSpa(spa =>
            {
                spa.Options.SourcePath = "../ImageStore.Frontend";

                if (env.IsDevelopment() && env.ShouldRunAngular())
                {
                    spa.UseAngularCliServer(npmScript: "start");
                }
            });

            if (env.IsDevelopment())
            {
                DbInitializer.Seed(context, serviceProvider).Wait();
            }
        }