using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Authorization; namespace ZB.MOM.WW.MxGateway.Server.Dashboard; /// /// Extension methods for configuring the gateway dashboard services. /// public static class DashboardServiceCollectionExtensions { /// /// Registers all dashboard services, authentication, and Razor components. /// /// Service collection to register services. public static IServiceCollection AddGatewayDashboard(this IServiceCollection services) { services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddScoped(); services.AddSingleton(); services.AddHostedService(); services.AddHostedService(); services.AddHttpContextAccessor(); services.AddAntiforgery(); services.AddCascadingAuthenticationState(); services.AddRazorComponents() .AddInteractiveServerComponents(); services.AddSignalR(); services .AddAuthentication(DashboardAuthenticationDefaults.AuthenticationScheme) .AddCookie(DashboardAuthenticationDefaults.AuthenticationScheme, cookieOptions => { cookieOptions.Cookie.Name = DashboardAuthenticationDefaults.CookieName; cookieOptions.Cookie.HttpOnly = true; cookieOptions.Cookie.SecurePolicy = CookieSecurePolicy.Always; cookieOptions.Cookie.SameSite = SameSiteMode.Strict; cookieOptions.Cookie.Path = "/"; cookieOptions.LoginPath = "/login"; cookieOptions.LogoutPath = "/logout"; cookieOptions.AccessDeniedPath = "/denied"; cookieOptions.ExpireTimeSpan = TimeSpan.FromHours(8); cookieOptions.SlidingExpiration = true; }) .AddScheme( DashboardAuthenticationDefaults.HubAuthenticationScheme, _ => { }); services.AddAuthorization(authorization => { authorization.AddPolicy( DashboardAuthenticationDefaults.ViewerPolicy, policy => policy.AddRequirements(DashboardAuthorizationRequirement.AnyDashboardRole)); authorization.AddPolicy( DashboardAuthenticationDefaults.AdminPolicy, policy => policy.AddRequirements(DashboardAuthorizationRequirement.AdminOnly)); authorization.AddPolicy( DashboardAuthenticationDefaults.HubClientsPolicy, policy => policy .AddAuthenticationSchemes( DashboardAuthenticationDefaults.AuthenticationScheme, DashboardAuthenticationDefaults.HubAuthenticationScheme) .AddRequirements(DashboardAuthorizationRequirement.AnyDashboardRole)); }); services.AddSingleton(); return services; } }