birbmap/Birdmap.API/Startup.cs

109 lines
3.5 KiB
C#
Raw Normal View History

using AutoMapper;
2020-10-31 15:57:19 +01:00
using Birdmap.API;
using Birdmap.API.Middlewares;
using Birdmap.BLL;
using Birdmap.DAL;
2020-10-21 17:04:54 +02:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
2020-10-21 11:05:17 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-10-21 17:04:54 +02:00
using Microsoft.IdentityModel.Tokens;
using System.Text;
2020-10-21 11:05:17 +02:00
namespace Birdmap
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
2020-10-21 17:04:54 +02:00
services.AddControllersWithViews()
.AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
opt.JsonSerializerOptions.IgnoreNullValues = true;
2020-10-21 17:04:54 +02:00
//opt.JsonSerializerOptions.PropertyNamingPolicy = new JsonNamingPolicy()
});
services.ConfigureBLL(Configuration);
services.ConfigureDAL(Configuration);
2020-10-21 17:04:54 +02:00
services.AddAutoMapper(typeof(Startup));
var key = Encoding.ASCII.GetBytes(Configuration["Secret"]);
2020-10-21 17:04:54 +02:00
services.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(opt =>
{
// opt.RequireHttpsMetadata = false;
opt.SaveToken = true;
opt.IncludeErrorDetails = true;
opt.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
2020-10-21 11:05:17 +02:00
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
// 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.UseMiddleware<ExceptionHandlerMiddleware>();
2020-10-21 11:05:17 +02:00
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseRouting();
2020-10-21 17:04:54 +02:00
app.UseAuthorization();
app.UseAuthentication();
2020-10-21 11:05:17 +02:00
app.UseEndpoints(endpoints =>
{
endpoints.MapHealthChecks("/health");
2020-10-21 17:04:54 +02:00
endpoints.MapControllers();
2020-10-21 11:05:17 +02:00
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseReactDevelopmentServer(npmScript: "start");
}
});
}
}
}