2023-11-18 12:41:14 +00:00
|
|
|
using JustShortIt.Components;
|
2023-04-15 13:40:46 +00:00
|
|
|
using JustShortIt.Model;
|
|
|
|
using JustShortIt.Service;
|
|
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
2023-11-18 13:04:51 +00:00
|
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
|
|
using Microsoft.AspNetCore.Components.Server;
|
2023-04-15 13:40:46 +00:00
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Configuration.AddEnvironmentVariables("JSI_");
|
|
|
|
|
2023-11-18 12:41:14 +00:00
|
|
|
builder.Services.AddAntiforgery();
|
|
|
|
builder.Services.AddRazorComponents();
|
|
|
|
|
2023-04-15 13:40:46 +00:00
|
|
|
// Get Configurations
|
|
|
|
var redisConnection = builder.Configuration.GetSection("Redis").Get<RedisConnection>();
|
|
|
|
var user = builder.Configuration.GetSection("Account").Get<User>();
|
|
|
|
string? baseUrl = builder.Configuration.GetValue<string>("BaseUrl");
|
2024-03-29 18:51:26 +00:00
|
|
|
builder.Services.Configure<Customization>(builder.Configuration.GetSection(nameof(Customization)));
|
2023-04-15 13:40:46 +00:00
|
|
|
|
2023-04-17 20:28:36 +00:00
|
|
|
#if DEBUG
|
|
|
|
baseUrl = "http://localhost/";
|
|
|
|
user = new User("test", "test");
|
|
|
|
Console.Error.WriteLine("YOU ARE RUNNING A DEBUG BUILD WITH TEST CREDENTIALS, " +
|
|
|
|
"DO NOT UNDER ANY CIRCUMSTANCES RUN THIS IN PRODUCTION, YOU HAVE BEEN WARNED.");
|
|
|
|
#endif
|
|
|
|
|
2023-04-15 13:40:46 +00:00
|
|
|
// Check if everything is configured (right)
|
|
|
|
if (string.IsNullOrEmpty(baseUrl) || Uri.IsWellFormedUriString(baseUrl, UriKind.Absolute) is false)
|
2023-04-17 20:28:36 +00:00
|
|
|
throw new ApplicationException(
|
|
|
|
"Base-URL is not set to a correct URL, please provide JSI_BaseUrl with a valid url.");
|
2023-04-15 13:40:46 +00:00
|
|
|
if (user is null || string.IsNullOrEmpty(user.Username) || string.IsNullOrEmpty(user.Password))
|
|
|
|
throw new ApplicationException(
|
|
|
|
"Credentials not set, please provide JSI_Account__Username and JSI_Account__Password.");
|
|
|
|
|
|
|
|
// Set up Distributed Cache
|
|
|
|
if (string.IsNullOrEmpty(redisConnection?.ConnectionString) is false) {
|
|
|
|
builder.Services.AddStackExchangeRedisCache(options => {
|
|
|
|
options.Configuration = redisConnection.ConnectionString;
|
|
|
|
options.InstanceName = redisConnection.InstanceName;
|
|
|
|
});
|
|
|
|
Console.WriteLine("Running with Redis distributed Cache.");
|
|
|
|
} else {
|
|
|
|
builder.Services.AddDistributedMemoryCache();
|
|
|
|
Console.WriteLine("Running with in-memory distributed Cache.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add Authentication
|
2023-11-18 13:04:51 +00:00
|
|
|
builder.Services.AddScoped(_ => new AuthenticationService(user));
|
2023-04-15 13:40:46 +00:00
|
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(options => {
|
|
|
|
options.ExpireTimeSpan = TimeSpan.FromHours(24);
|
|
|
|
options.SlidingExpiration = true;
|
|
|
|
options.AccessDeniedPath = "/Login";
|
|
|
|
options.LoginPath = "/Login";
|
|
|
|
options.LogoutPath = "/Logout";
|
|
|
|
});
|
2023-11-18 15:54:53 +00:00
|
|
|
builder.Services.AddAuthorization();
|
2023-11-18 13:04:51 +00:00
|
|
|
builder.Services.AddScoped<AuthenticationStateProvider, ServerAuthenticationStateProvider>();
|
|
|
|
builder.Services.AddCascadingAuthenticationState();
|
2023-11-18 15:24:32 +00:00
|
|
|
builder.Services.AddHttpContextAccessor();
|
2023-04-15 13:40:46 +00:00
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
// Configure Cookies (used in Authentication)
|
|
|
|
app.UseCookiePolicy(new CookiePolicyOptions {
|
|
|
|
MinimumSameSitePolicy = SameSiteMode.Strict,
|
|
|
|
Secure = CookieSecurePolicy.Always
|
|
|
|
});
|
|
|
|
|
|
|
|
// Configure the HTTP request pipeline.
|
|
|
|
if (!app.Environment.IsDevelopment()) {
|
2023-11-18 15:41:38 +00:00
|
|
|
app.UseExceptionHandler("/error", createScopeForErrors: true);
|
2023-04-15 13:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
app.UseAuthorization();
|
2023-11-18 12:41:14 +00:00
|
|
|
app.UseAntiforgery();
|
|
|
|
app.MapRazorComponents<App>();
|
2023-04-15 13:40:46 +00:00
|
|
|
app.Run();
|