tuitio/IdentityServer.Api/Startup.cs

90 lines
2.9 KiB
C#
Raw Normal View History

2020-12-20 03:22:27 +02:00
using IdentityServer.Application;
2021-11-13 17:17:13 +02:00
using IdentityServer.Application.Services.Abstractions;
2020-12-20 03:22:27 +02:00
using IdentityServer.Domain.Data;
using MediatR;
using MediatR.Pipeline;
2020-12-19 18:17:24 +02:00
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
2020-12-20 22:50:01 +02:00
using NDB.Extensions.Swagger;
using NDB.Extensions.Swagger.Constants;
using NDB.Infrastructure.DatabaseMigration;
using NDB.Infrastructure.DatabaseMigration.Constants;
2020-12-20 03:22:27 +02:00
using Newtonsoft.Json;
using System.Reflection;
2020-12-19 18:17:24 +02:00
namespace IdentityServer.Api
{
public class Startup
{
2020-12-20 03:22:27 +02:00
private readonly IConfiguration _configuration;
2020-12-19 18:17:24 +02:00
public Startup(IConfiguration configuration)
{
2020-12-20 03:22:27 +02:00
_configuration = configuration;
2020-12-19 18:17:24 +02:00
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
2020-12-20 03:22:27 +02:00
services.AddControllers()
.AddNewtonsoftJson(o => o.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
// MediatR
services.AddMediatR(GetMediatRAssemblies());
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPreProcessorBehavior<,>));
services.AddScoped(typeof(IPipelineBehavior<,>), typeof(RequestPostProcessorBehavior<,>));
// AutoMapper
services.AddAutoMapper(
typeof(Application.Mappings.MappingProfile).Assembly);
// Swagger
2020-12-20 22:50:01 +02:00
services.AddSwagger("Identity Server API", AuthorizationType.None);
2020-12-20 03:22:27 +02:00
// Data access
services.AddMigration(DatabaseType.SQLServer, MetadataLocation.Database);
2020-12-20 03:22:27 +02:00
services.AddDataAccess();
// Application
services.AddApplicationServices();
}
private Assembly[] GetMediatRAssemblies()
{
var assembly = typeof(Application.Commands.AuthenticateUser).Assembly;
return new Assembly[] { assembly };
2020-12-19 18:17:24 +02:00
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
2020-12-20 03:22:27 +02:00
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
2020-12-19 18:17:24 +02:00
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
2020-12-20 22:50:01 +02:00
app.ConfigureSwagger("IdentityServer API");
2021-11-13 17:17:13 +02:00
app.UseMigration();
2021-11-13 17:17:13 +02:00
var behaviorService = app.ApplicationServices.GetService<IBehaviorService>();
behaviorService.FillTokenStore();
2020-12-19 18:17:24 +02:00
}
}
}