using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using SignalRChat.Hubs; using MediatR.Pipeline; using Sledgemapper.Api.Data; using Microsoft.EntityFrameworkCore; using MediatR; using Microsoft.IdentityModel.Tokens; using Sledgemapper.Helpers; using Microsoft.AspNetCore.Authentication.JwtBearer; using System.Text; using AutoMapper; using Sledgemapper.Services; using System.Security.Claims; namespace SignalRChat { public class Startup { private readonly IWebHostEnvironment _env; public IConfiguration Configuration { get; } public Startup(IWebHostEnvironment env, IConfiguration configuration) { Configuration = configuration; _env = env; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { if (_env.IsProduction()) services.AddDbContext(); else services.AddDbContext(); // services.AddRazorPages(); services.AddCors(); services.AddControllers(); services.AddSignalR(); services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); services.AddMediatR(typeof(Startup)); services.AddDbContext(options => {options.UseSqlite("Data Source=sledgemapper.db"); options.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);}); // services.AddEntityFrameworkSqlite().AddDbContext(); // configure strongly typed settings objects var c = Configuration.GetSection("AppSettings"); var appSettingsSection = Configuration.GetSection("AppSettings"); services.Configure(appSettingsSection); // configure jwt authentication var appSettings = appSettingsSection.Get(); var key = Encoding.ASCII.GetBytes(appSettings.Secret); services.AddAuthentication(x => { x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(x => { x.Events = new JwtBearerEvents { OnTokenValidated = context => { var userService = context.HttpContext.RequestServices.GetRequiredService(); var userId = int.Parse(context.Principal.Identity.Name); var user = userService.GetById(userId); if (user == null) { // return unauthorized if user no longer exists context.Fail("Unauthorized"); } context.HttpContext.User.Claims.Append(new Claim(ClaimTypes.Name, user.Username)); context.HttpContext.User.Claims.Append(new Claim(ClaimTypes.NameIdentifier, user.Id.ToString())); return Task.CompletedTask; } }; x.RequireHttpsMetadata = false; x.SaveToken = true; x.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(key), ValidateIssuer = false, ValidateAudience = false }; }); // configure DI for application services services.AddScoped(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext) { dataContext.Database.Migrate(); // 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.UseRouting(); // global cors policy app.UseCors(x => x .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseEndpoints(endpoints => { // endpoints.MapRazorPages(); endpoints.MapHub("/sledgemapperhub"); }); } } }