Skip to content
Permalink
main
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
using System;
using System.Linq;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Logging;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using Serilog;
using WebApi.Domain;
using WebApi.Extensions;
namespace WebApi
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
this.Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o =>
{
o.AddPolicy("AllowAllOrigins", builder =>
{
builder
.WithOrigins("http://localhost:4200")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials()
.WithExposedHeaders("X-Pagination");
});
});
services.AddRouting(o => o.LowercaseUrls = true);
var authority = this.Configuration["Authority"];
services
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(opt =>
{
opt.Authority = authority;
opt.Audience = "api1";
opt.RequireHttpsMetadata = false;
opt.IncludeErrorDetails = true;
opt.SaveToken = true;
opt.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = false,
NameClaimType = "name",
// RoleClaimType = "role" // role based policies will not work if uncommented!
};
});
var connection = this.Configuration["ConnectionString"];
services.AddDbContext<DomainContext>(o => o.UseSqlite(connection));
// Swagger
services.AddSwaggerDocumentation();
// Api services
services.AddApiServices<DomainContext>(this.Configuration);
services.AddHttpContextAccessor();
services.AddControllers()
.AddNewtonsoftJson(opt =>
{
opt.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
}
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env
)
{
if (env.IsDevelopment())
{
app.UseCors("AllowAllOrigins");
app.UseSwaggerDocumentation();
IdentityModelEventSource.ShowPII = true;
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
ConsiderSpaRoutes(app);
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseSerilogRequestLogging();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
private static void ConsiderSpaRoutes(IApplicationBuilder app)
{
var angularRoutes = new[]
{
"/home",
"/dispatch",
"/admin",
"/issue",
"/dispatch",
"/forbidden"
};
app.Use(async (context, next) =>
{
if (context.Request.Path.HasValue
&& null != angularRoutes.FirstOrDefault(
(ar) => context.Request.Path.Value.StartsWith(ar, StringComparison.OrdinalIgnoreCase)))
{
context.Request.Path = new PathString("/");
}
await next();
});
}
}
}