diff --git a/Tsi1.Api/Tsi1.Api/Startup.cs b/Tsi1.Api/Tsi1.Api/Startup.cs index 426205b744af3761ec3ac4452ae6f9da6673448c..854ba8a94b7a335050dcd3c5d35f1e8c2fa73939 100644 --- a/Tsi1.Api/Tsi1.Api/Startup.cs +++ b/Tsi1.Api/Tsi1.Api/Startup.cs @@ -6,6 +6,7 @@ using System.Text; using System.Threading.Tasks; using AutoMapper; using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; @@ -14,6 +15,7 @@ using Microsoft.AspNetCore.StaticFiles; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; @@ -79,7 +81,14 @@ namespace Tsi1.Api services.AddScoped<IEmailService, EmailService>(); services.AddCors(); - + + services.AddAuthorization(options => + { + options.FallbackPolicy = new AuthorizationPolicyBuilder() + .RequireAuthenticatedUser() + .Build(); + }); + services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; @@ -151,14 +160,6 @@ namespace Tsi1.Api c.DocumentTitle = "Tsi1 api"; }); - var provider = new FileExtensionContentTypeProvider(); - app.UseStaticFiles(new StaticFileOptions() - { - ContentTypeProvider = provider, - RequestPath = "/static" - } - ); ; - app.UseRouting(); app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); @@ -166,6 +167,16 @@ namespace Tsi1.Api app.UseAuthentication(); app.UseAuthorization(); + var provider = new FileExtensionContentTypeProvider(); + app.UseStaticFiles(new StaticFileOptions() + { + ContentTypeProvider = provider, + FileProvider = new PhysicalFileProvider( + Path.Combine(env.ContentRootPath, "StaticFiles")), + RequestPath = "/static" + } + ); + app.UseEndpoints(endpoints => { endpoints.MapControllers(); diff --git a/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj b/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj index 1ed4b443527a84acaf750eab3427ac11002a946a..7fa4dab4f5b7683e724fdac0596793f7c778db45 100644 --- a/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj +++ b/Tsi1.Api/Tsi1.Api/Tsi1.Api.csproj @@ -14,5 +14,9 @@ <ProjectReference Include="..\Tsi1.BusinessLayer\Tsi1.BusinessLayer.csproj" /> </ItemGroup> + <ItemGroup> + <Folder Include="StaticFiles\" /> + </ItemGroup> + </Project> diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs index eaa3d746ce68273af4e123f08cc123d300bd5a32..5e7fcd662caf3d89f21273a38e9908785d60750e 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/FileService.cs @@ -27,7 +27,7 @@ namespace Tsi1.BusinessLayer.Services { _context = context; _mapper = mapper; - _path = hostingEnvironment.ContentRootPath + "/wwwroot"; + _path = hostingEnvironment.ContentRootPath + "/StaticFiles"; } public async Task<ServiceResult<string>> SaveFile(IFormFile file, string filePath)