2020-10-21 17:04:54 +02:00
|
|
|
using Birdmap.Services;
|
|
|
|
using Birdmap.Services.Interfaces;
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2020-10-21 11:05:17 +02:00
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
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;
|
|
|
|
using System.Text;
|
|
|
|
using System.Text.Json;
|
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.PropertyNamingPolicy = new JsonNamingPolicy()
|
|
|
|
});
|
|
|
|
|
|
|
|
services.AddTransient<IAuthService, AuthService>();
|
|
|
|
|
|
|
|
var key = Encoding.ASCII.GetBytes(Configuration["BasicAuth:Secret"]);
|
|
|
|
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.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();
|
|
|
|
}
|
|
|
|
|
|
|
|
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 =>
|
|
|
|
{
|
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");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|