using System.Linq; using System.Text; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using AutoMapper; using skydiveLogs_api.Ioc; namespace skydiveLogs_api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc(options => { options.EnableEndpointRouting = false; }) .SetCompatibilityVersion(CompatibilityVersion.Version_3_0); // JWT var jwtConf = Configuration.GetSection("JWT") .GetChildren() .ToDictionary(d => d.Key, d => d.Value); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = jwtConf["Issuer"], ValidAudience = jwtConf["Issuer"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtConf["Key"])) }; }); // CORS var corsConf = Configuration.GetSection("Cors") .GetChildren() .ToDictionary(d => d.Key, d => d.Value); services.AddCors(options => { options.AddDefaultPolicy( builder => { builder.WithOrigins(corsConf["FrontUrl"]) .AllowAnyHeader() .AllowAnyMethod(); }); }); // IoC var iocService = new IocService(services, Configuration); iocService.Configure(); // AutoMapper services.AddAutoMapper(typeof(Mapper.ModelProfile)); } // 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.EnvironmentName == "Development") { app.UseDeveloperExceptionPage(); } else { // 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.UseCors(); app.UseAuthentication(); app.UseMvc(); } public IConfiguration Configuration { get; } } }