using Discord; using Discord.Commands; using Discord.OAuth2; using Discord.WebSocket; using Lieb.Data; using Lieb.Discord; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.EntityFrameworkCore; using Microsoft.Net.Http.Headers; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); #if DEBUG builder.Services.AddDbContextFactory(opt => //opt.UseSqlServer(builder.Configuration.GetConnectionString("LiebContext")).EnableSensitiveDataLogging(), ServiceLifetime.Transient); opt.UseSqlite(builder.Configuration.GetConnectionString("LiebContext"))); #else builder.Services.AddDbContextFactory(opt => opt.UseMySql(builder.Configuration.GetConnectionString("LiebContext"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("LiebContext"))), ServiceLifetime.Transient); #endif builder.Services.AddDatabaseDeveloperPageExceptionFilter(); builder.Services.AddServerSideBlazor(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddHostedService(); /* #region discord //DiscordSocketClient client = new DiscordSocketClient(); //client.Log += Log; var token = File.ReadAllText("token.txt"); await client.LoginAsync(TokenType.Bot, token); await client.StartAsync(); builder.Services.AddSingleton(client); builder.Services.AddSingleton(); builder.Services.AddSingleton(); #endregion discord*/ builder.Services.AddAuthentication(opt => { opt.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; opt.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme; opt.DefaultChallengeScheme = DiscordDefaults.AuthenticationScheme; }) .AddCookie() .AddDiscord(x => { x.AppId = builder.Configuration["Discord:AppId"]; x.AppSecret = builder.Configuration["Discord:AppSecret"]; x.SaveTokens = true; }); builder.Services.AddAuthorization(options => { options.AddPolicy(Constants.Roles.User.Name, policy => policy.RequireClaim(Constants.ClaimType, new List(){ Constants.Roles.User.Name, Constants.Roles.RaidLead.Name, Constants.Roles.Moderator.Name, Constants.Roles.Admin.Name})); options.AddPolicy(Constants.Roles.RaidLead.Name, policy => policy.RequireClaim(Constants.ClaimType, new List() { Constants.Roles.RaidLead.Name, Constants.Roles.Moderator.Name, Constants.Roles.Admin.Name })); options.AddPolicy(Constants.Roles.Moderator.Name, policy => policy.RequireClaim(Constants.ClaimType, new List() { Constants.Roles.Moderator.Name, Constants.Roles.Admin.Name })); options.AddPolicy(Constants.Roles.Admin.Name, policy => policy.RequireClaim(Constants.ClaimType, new List() { Constants.Roles.Admin.Name })); }); builder.Services.AddHttpClient(Constants.HttpClientName , httpClient => { httpClient.BaseAddress = new Uri("http://localhost:5240/"); // using Microsoft.Net.Http.Headers; // The GitHub API requires two headers. httpClient.DefaultRequestHeaders.Add( HeaderNames.Accept, "application/vnd.github.v3+json"); httpClient.DefaultRequestHeaders.Add( HeaderNames.UserAgent, "HttpRequestsSample"); }).ConfigurePrimaryHttpMessageHandler(() => { var handler = new HttpClientHandler(); if (builder.Environment.IsDevelopment()) { handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; } return handler; }); var app = builder.Build(); // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { 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(); } else { app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); } using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; var context = services.GetRequiredService(); context.Database.EnsureCreated(); DbInitializer.Initialize(context); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapDefaultControllerRoute(); }); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); app.Run();