using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using ReallifeGamemode.Database.Models; using ReallifeGamemode.DataService.Logic; using Swashbuckle.AspNetCore.Swagger; namespace ReallifeGamemode.DataService { 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) { services.Configure(cfg => Configuration.Bind(cfg)); services.AddDbContext(db => { db.UseMySql(Configuration["ConnectionString"]); }); services.AddLogic(); services .AddMvc() .AddJsonOptions(j => { j.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; j.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc; j.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.IsoDateFormat; }); var tokenKey = Encoding.UTF8.GetBytes(Configuration["TokenSecret"]); services.AddAuthentication(o => { o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(o => { o.RequireHttpsMetadata = false; o.SaveToken = false; o.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(tokenKey), ValidateIssuer = true, ValidIssuer = "LOGDATASERVICE", ValidateAudience = false, ValidateLifetime = true }; }); string debugToken = null; #if DEBUG debugToken = services.BuildServiceProvider().GetService().GetDebugToken(tokenKey); #endif services.AddSwaggerGen(c => { Info info = new Info { Title = "GTA:V ControlPanl DataService", Version = "0.1", }; if(debugToken != null) { info.Description = $"Debug-Token: {debugToken}"; } c.SwaggerDoc("DataService", info); c.AddSecurityDefinition("Bearer", new ApiKeyScheme { Name = "Authorization", In = "Header", Type = "apiKey" }); c.AddSecurityRequirement(new Dictionary>() { { "Bearer", new string[] { } } }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseAuthentication(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseSwaggerUI(c => { c.RoutePrefix = "doc"; c.SwaggerEndpoint("DataService.json", "DataService"); }); app.UseSwagger(c => { c.RouteTemplate = "doc/{documentName}.json"; }); app.UseMvc(); } } }