Recreated solution with web app template. Improved theme. Switched to ShadcnBlazor library
This commit is contained in:
10
Moonlight.Api/Startup/IAppStartup.cs
Normal file
10
Moonlight.Api/Startup/IAppStartup.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Moonlight.Api.Startup;
|
||||
|
||||
public interface IAppStartup
|
||||
{
|
||||
public void PreBuild(WebApplicationBuilder builder);
|
||||
public void PostBuild(WebApplication application);
|
||||
public void PostMiddleware(WebApplication application);
|
||||
}
|
||||
91
Moonlight.Api/Startup/Startup.Auth.cs
Normal file
91
Moonlight.Api/Startup/Startup.Auth.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moonlight.Api.Configuration;
|
||||
using Moonlight.Api.Services;
|
||||
|
||||
namespace Moonlight.Api.Startup;
|
||||
|
||||
public partial class Startup
|
||||
{
|
||||
private static void AddAuth(WebApplicationBuilder builder)
|
||||
{
|
||||
var oidcOptions = new OidcOptions();
|
||||
builder.Configuration.GetSection("WebApp:Oidc").Bind(oidcOptions);
|
||||
|
||||
builder.Services.AddScoped<UserAuthService>();
|
||||
|
||||
builder.Services.AddAuthentication("Session")
|
||||
.AddCookie("Session", null, options =>
|
||||
{
|
||||
options.Events.OnSigningIn += async context =>
|
||||
{
|
||||
var authService = context
|
||||
.HttpContext
|
||||
.RequestServices
|
||||
.GetRequiredService<UserAuthService>();
|
||||
|
||||
var result = await authService.SyncAsync(context.Principal);
|
||||
|
||||
if (result)
|
||||
context.Properties.IsPersistent = true;
|
||||
else
|
||||
context.Principal = new ClaimsPrincipal();
|
||||
};
|
||||
|
||||
options.Events.OnValidatePrincipal += async context =>
|
||||
{
|
||||
var authService = context
|
||||
.HttpContext
|
||||
.RequestServices
|
||||
.GetRequiredService<UserAuthService>();
|
||||
|
||||
var result = await authService.ValidateAsync(context.Principal);
|
||||
|
||||
if (!result)
|
||||
context.RejectPrincipal();
|
||||
};
|
||||
|
||||
options.Cookie = new CookieBuilder()
|
||||
{
|
||||
Name = "token",
|
||||
Path = "/",
|
||||
IsEssential = true,
|
||||
SecurePolicy = CookieSecurePolicy.SameAsRequest
|
||||
};
|
||||
})
|
||||
.AddOpenIdConnect("OIDC", "OpenID Connect", options =>
|
||||
{
|
||||
options.Authority = oidcOptions.Authority;
|
||||
options.RequireHttpsMetadata = oidcOptions.RequireHttpsMetadata;
|
||||
|
||||
var scopes = oidcOptions.Scopes ?? ["openid", "email", "profile"];
|
||||
|
||||
options.Scope.Clear();
|
||||
|
||||
foreach (var scope in scopes)
|
||||
options.Scope.Add(scope);
|
||||
|
||||
options.ResponseType = oidcOptions.ResponseType;
|
||||
options.ClientId = oidcOptions.ClientId;
|
||||
options.ClientSecret = oidcOptions.ClientSecret;
|
||||
|
||||
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
|
||||
options.ClaimActions.MapJsonKey(ClaimTypes.Name, "preferred_username");
|
||||
options.ClaimActions.MapJsonKey(ClaimTypes.Email, "email");
|
||||
|
||||
options.GetClaimsFromUserInfoEndpoint = true;
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
}
|
||||
|
||||
private static void UseAuth(WebApplication application)
|
||||
{
|
||||
application.UseAuthentication();
|
||||
application.UseAuthorization();
|
||||
}
|
||||
}
|
||||
36
Moonlight.Api/Startup/Startup.Base.cs
Normal file
36
Moonlight.Api/Startup/Startup.Base.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Console;
|
||||
using Moonlight.Shared.Http;
|
||||
using Moonlight.Api.Helpers;
|
||||
|
||||
namespace Moonlight.Api.Startup;
|
||||
|
||||
public partial class Startup
|
||||
{
|
||||
private static void AddBase(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddControllers().AddJsonOptions(options =>
|
||||
{
|
||||
options.JsonSerializerOptions.TypeInfoResolverChain.Add(SerializationContext.Default);
|
||||
});
|
||||
|
||||
builder.Logging.ClearProviders();
|
||||
builder.Logging.AddConsole(options => { options.FormatterName = nameof(AppConsoleFormatter); });
|
||||
builder.Logging.AddConsoleFormatter<AppConsoleFormatter, ConsoleFormatterOptions>();
|
||||
}
|
||||
|
||||
private static void UseBase(WebApplication application)
|
||||
{
|
||||
|
||||
application.UseRouting();
|
||||
}
|
||||
|
||||
private static void MapBase(WebApplication application)
|
||||
{
|
||||
application.MapControllers();
|
||||
|
||||
application.MapFallbackToFile("index.html");
|
||||
}
|
||||
}
|
||||
19
Moonlight.Api/Startup/Startup.Database.cs
Normal file
19
Moonlight.Api/Startup/Startup.Database.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moonlight.Api.Configuration;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Services;
|
||||
|
||||
namespace Moonlight.Api.Startup;
|
||||
|
||||
public partial class Startup
|
||||
{
|
||||
private static void AddDatabase(WebApplicationBuilder builder)
|
||||
{
|
||||
builder.Services.AddOptions<DatabaseOptions>().BindConfiguration("WebApp:Database");
|
||||
|
||||
builder.Services.AddDbContext<DataContext>();
|
||||
builder.Services.AddScoped(typeof(DatabaseRepository<>));
|
||||
builder.Services.AddHostedService<DbMigrationService>();
|
||||
}
|
||||
}
|
||||
24
Moonlight.Api/Startup/Startup.cs
Normal file
24
Moonlight.Api/Startup/Startup.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
|
||||
namespace Moonlight.Api.Startup;
|
||||
|
||||
public partial class Startup : IAppStartup
|
||||
{
|
||||
public void PreBuild(WebApplicationBuilder builder)
|
||||
{
|
||||
AddBase(builder);
|
||||
AddAuth(builder);
|
||||
AddDatabase(builder);
|
||||
}
|
||||
|
||||
public void PostBuild(WebApplication application)
|
||||
{
|
||||
UseBase(application);
|
||||
UseAuth(application);
|
||||
}
|
||||
|
||||
public void PostMiddleware(WebApplication application)
|
||||
{
|
||||
MapBase(application);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user