2018-04-08 02:44:42 -04:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
2019-02-16 18:18:50 -05:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.ApplicationParts;
|
2018-05-28 21:30:31 -04:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Razor;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-04-08 02:44:42 -04:00
|
|
|
|
using SharedLibraryCore.Database;
|
2019-12-02 16:52:36 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-02-12 21:34:29 -05:00
|
|
|
|
using WebfrontCore.Middleware;
|
2018-02-21 20:29:23 -05:00
|
|
|
|
|
|
|
|
|
namespace WebfrontCore
|
|
|
|
|
{
|
|
|
|
|
public class Startup
|
|
|
|
|
{
|
2018-04-08 02:44:42 -04:00
|
|
|
|
public static IConfigurationRoot Configuration { get; private set; }
|
|
|
|
|
|
2019-09-30 19:35:36 -04:00
|
|
|
|
public Startup(IWebHostEnvironment env)
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
|
|
|
|
var builder = new ConfigurationBuilder()
|
|
|
|
|
.AddEnvironmentVariables();
|
2018-04-06 20:15:17 -04:00
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
Configuration = builder.Build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
{
|
2019-03-27 20:40:26 -04:00
|
|
|
|
// allow CORS
|
|
|
|
|
services.AddCors(_options =>
|
|
|
|
|
{
|
|
|
|
|
_options.AddPolicy("AllowAll",
|
|
|
|
|
_builder =>
|
|
|
|
|
{
|
|
|
|
|
_builder.AllowAnyOrigin()
|
|
|
|
|
.AllowAnyMethod()
|
2019-09-30 19:35:36 -04:00
|
|
|
|
.AllowAnyHeader();
|
2019-03-27 20:40:26 -04:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
// Add framework services.
|
2019-09-30 19:35:36 -04:00
|
|
|
|
var mvcBuilder = services.AddMvc(_options => _options.SuppressAsyncSuffixInActionNames = false)
|
2019-02-16 18:18:50 -05:00
|
|
|
|
.ConfigureApplicationPartManager(_ =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var assembly in Program.Manager.GetPluginAssemblies())
|
|
|
|
|
{
|
|
|
|
|
if (assembly.FullName.Contains("Views"))
|
|
|
|
|
{
|
|
|
|
|
_.ApplicationParts.Add(new CompiledRazorAssemblyPart(assembly));
|
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-02-16 18:18:50 -05:00
|
|
|
|
else if (assembly.FullName.Contains("Web"))
|
|
|
|
|
{
|
|
|
|
|
_.ApplicationParts.Add(new AssemblyPart(assembly));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-02-25 20:36:10 -05:00
|
|
|
|
#if DEBUG
|
2019-12-02 16:52:36 -05:00
|
|
|
|
//mvcBuilder = mvcBuilder.AddRazorRuntimeCompilation();
|
2019-02-25 20:36:10 -05:00
|
|
|
|
services.Configure<RazorViewEngineOptions>(_options =>
|
|
|
|
|
{
|
|
|
|
|
_options.ViewLocationFormats.Add(@"/Views/Plugins/{1}/{0}" + RazorViewEngine.ViewExtension);
|
|
|
|
|
});
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-02-16 18:18:50 -05:00
|
|
|
|
foreach (var asm in Program.Manager.GetPluginAssemblies())
|
2018-05-28 21:30:31 -04:00
|
|
|
|
{
|
2019-02-16 18:18:50 -05:00
|
|
|
|
mvcBuilder.AddApplicationPart(asm);
|
|
|
|
|
}
|
2018-05-28 21:30:31 -04:00
|
|
|
|
|
2019-08-04 21:38:55 -04:00
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
|
2018-04-08 02:44:42 -04:00
|
|
|
|
services.AddEntityFrameworkSqlite()
|
|
|
|
|
.AddDbContext<DatabaseContext>();
|
|
|
|
|
|
|
|
|
|
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
|
|
|
|
|
{
|
|
|
|
|
options.AccessDeniedPath = "/";
|
|
|
|
|
options.LoginPath = "/";
|
|
|
|
|
});
|
2019-02-12 21:34:29 -05:00
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
services.AddLogging(_builder =>
|
|
|
|
|
{
|
|
|
|
|
_builder.AddDebug();
|
|
|
|
|
});
|
|
|
|
|
#endif
|
2019-12-02 16:52:36 -05:00
|
|
|
|
|
|
|
|
|
services.AddSingleton(Program.Manager);
|
2018-02-21 20:29:23 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
2019-09-30 19:35:36 -04:00
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2019-10-23 14:35:20 -04:00
|
|
|
|
app.UseStatusCodePagesWithRedirects("/Home/ResponseStatusCode?statusCode={0}");
|
|
|
|
|
|
2019-09-30 19:35:36 -04:00
|
|
|
|
if (env.EnvironmentName == "Development")
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
}
|
2019-01-26 21:33:37 -05:00
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
app.UseExceptionHandler("/Home/Error");
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-12 21:34:29 -05:00
|
|
|
|
if (Program.Manager.GetApplicationSettings().Configuration().EnableWebfrontConnectionWhitelist)
|
|
|
|
|
{
|
|
|
|
|
app.UseMiddleware<IPWhitelist>(Program.Manager.GetApplicationSettings().Configuration().WebfrontConnectionWhitelist);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-21 20:29:23 -05:00
|
|
|
|
app.UseStaticFiles();
|
2018-04-08 02:44:42 -04:00
|
|
|
|
app.UseAuthentication();
|
2019-03-27 20:40:26 -04:00
|
|
|
|
app.UseCors("AllowAll");
|
2018-04-04 15:38:34 -04:00
|
|
|
|
|
2019-10-07 18:35:37 -04:00
|
|
|
|
// prevents banned/demoted users from keeping their claims
|
|
|
|
|
app.UseMiddleware<ClaimsPermissionRemoval>(Program.Manager);
|
|
|
|
|
|
2019-09-30 19:35:36 -04:00
|
|
|
|
app.UseRouting();
|
2019-12-25 22:05:57 -05:00
|
|
|
|
app.UseAuthorization();
|
2019-09-30 19:35:36 -04:00
|
|
|
|
app.UseEndpoints(endpoints =>
|
2018-02-21 20:29:23 -05:00
|
|
|
|
{
|
2019-09-30 19:35:36 -04:00
|
|
|
|
endpoints.MapControllerRoute("default", "{controller=Home}/{action=Index}/{id?}");
|
2018-02-21 20:29:23 -05:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|