From a97ed2efd23bebc8a33672d45d88bc4ca8a86d43 Mon Sep 17 00:00:00 2001 From: Jon P Smith Date: Wed, 28 Feb 2024 09:49:27 +0000 Subject: [PATCH] Example1 updated --- ...mple1.RazorPages.IndividualAccounts.csproj | 16 +- .../Program.cs | 221 +++++++++--------- 2 files changed, 115 insertions(+), 122 deletions(-) diff --git a/Example1.RazorPages.IndividualAccounts/Example1.RazorPages.IndividualAccounts.csproj b/Example1.RazorPages.IndividualAccounts/Example1.RazorPages.IndividualAccounts.csproj index 2de7557b..5595d54d 100644 --- a/Example1.RazorPages.IndividualAccounts/Example1.RazorPages.IndividualAccounts.csproj +++ b/Example1.RazorPages.IndividualAccounts/Example1.RazorPages.IndividualAccounts.csproj @@ -1,22 +1,22 @@  - net6.0 + net8.0 aspnet-Example1.RazorPages.IndividualAccounts-9BFCF434-1CBC-460C-98B7-CBCEDC0204D8 false - - - - - - + + + + + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Example1.RazorPages.IndividualAccounts/Program.cs b/Example1.RazorPages.IndividualAccounts/Program.cs index 6e3206c0..88ba3675 100644 --- a/Example1.RazorPages.IndividualAccounts/Program.cs +++ b/Example1.RazorPages.IndividualAccounts/Program.cs @@ -13,126 +13,119 @@ using AuthPermissions.AspNetCore; using AuthPermissions.AspNetCore.Services; using AuthPermissions.AspNetCore.StartupServices; +using Example1.RazorPages.IndividualAccounts; using Example1.RazorPages.IndividualAccounts.Data; using LocalizeMessagesAndErrors; using Microsoft.EntityFrameworkCore; using RunMethodsSequentially; using Microsoft.Extensions.Options; -namespace Example1.RazorPages.IndividualAccounts; -public class Program +var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddDatabaseDeveloperPageExceptionFilter(); + +builder.Services.AddDbContext(opt => + opt.UseInMemoryDatabase(nameof(ApplicationDbContext))); + +builder.Services.AddDefaultIdentity( + options => options.SignIn.RequireConfirmedAccount = false) + .AddEntityFrameworkStores(); + +//Example of configure a page as only shown if you log in +builder.Services.AddRazorPages(options => +{ + options.Conventions.AuthorizePage("/AuthBuiltIn/LoggedInConfigure"); +}) +#region localization + .AddViewLocalization(options => options.ResourcesPath = "Resources"); +#endregion + +#region localization - defining the cultures +//see https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization#localization-middleware +var supportedCultures = new[] { "en", "fr"}; +var localizationOptions = new RequestLocalizationOptions() + .SetDefaultCulture(supportedCultures[0]) + .AddSupportedCultures(supportedCultures) + .AddSupportedUICultures(supportedCultures); + + +//This defines that the culture is selected by the culture cookie +localizationOptions.RequestCultureProviders = new List() { - public static void Main(string[] args) + new CookieRequestCultureProvider(), + //new AcceptLanguageHeaderRequestCultureProvider(), + //new QueryStringRequestCultureProvider() +}; + +builder.Services.Configure(options => +{ + List supportedCultures = new List + { + new ("en"), + new ("fr"), + }; + options.DefaultRequestCulture = new RequestCulture("en"); + options.SupportedCultures = supportedCultures; + options.SupportedUICultures = supportedCultures; + + options.RequestCultureProviders = new List() + { + new CookieRequestCultureProvider(), + new AcceptLanguageHeaderRequestCultureProvider(), + new QueryStringRequestCultureProvider() + }; +}); +#endregion + +//Register the SimpleLocalizer with its own Resource file +//This is used for localization of simple messages +builder.Services.RegisterSimpleLocalizer(); + +builder.Services.RegisterAuthPermissions() + .UsingInMemoryDatabase() + .IndividualAccountsAuthentication() + .AddRolesPermissionsIfEmpty(AppAuthSetupData.RolesDefinition) + .AddAuthUsersIfEmpty(AppAuthSetupData.UsersWithRolesDefinition) + .RegisterAuthenticationProviderReader() + .RegisterFindUserInfoService() + .SetupAuthPLocalization(supportedCultures) + .AddSuperUserToIndividualAccounts() + .SetupAspNetCoreAndDatabase(options => { - var builder = WebApplication.CreateBuilder(args); - - builder.Services.AddDatabaseDeveloperPageExceptionFilter(); - - builder.Services.AddDbContext(opt => - opt.UseInMemoryDatabase(nameof(ApplicationDbContext))); - - builder.Services.AddDefaultIdentity( - options => options.SignIn.RequireConfirmedAccount = false) - .AddEntityFrameworkStores(); - - //Example of configure a page as only shown if you log in - builder.Services.AddRazorPages(options => - { - options.Conventions.AuthorizePage("/AuthBuiltIn/LoggedInConfigure"); - }) - #region localization - .AddViewLocalization(options => options.ResourcesPath = "Resources"); - #endregion - - #region localization - defining the cultures - //see https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization#localization-middleware - var supportedCultures = new[] { "en", "fr"}; - var localizationOptions = new RequestLocalizationOptions() - .SetDefaultCulture(supportedCultures[0]) - .AddSupportedCultures(supportedCultures) - .AddSupportedUICultures(supportedCultures); - - - //This defines that the culture is selected by the culture cookie - localizationOptions.RequestCultureProviders = new List() - { - new CookieRequestCultureProvider(), - //new AcceptLanguageHeaderRequestCultureProvider(), - //new QueryStringRequestCultureProvider() - }; - - builder.Services.Configure(options => - { - List supportedCultures = new List - { - new ("en"), - new ("fr"), - }; - options.DefaultRequestCulture = new RequestCulture("en"); - options.SupportedCultures = supportedCultures; - options.SupportedUICultures = supportedCultures; - - options.RequestCultureProviders = new List() - { - new CookieRequestCultureProvider(), - new AcceptLanguageHeaderRequestCultureProvider(), - new QueryStringRequestCultureProvider() - }; - }); - #endregion - - //Register the SimpleLocalizer with its own Resource file - //This is used for localization of simple messages - builder.Services.RegisterSimpleLocalizer(); - - builder.Services.RegisterAuthPermissions() - .UsingInMemoryDatabase() - .IndividualAccountsAuthentication() - .AddRolesPermissionsIfEmpty(AppAuthSetupData.RolesDefinition) - .AddAuthUsersIfEmpty(AppAuthSetupData.UsersWithRolesDefinition) - .RegisterAuthenticationProviderReader() - .RegisterFindUserInfoService() - .SetupAuthPLocalization(supportedCultures) - .AddSuperUserToIndividualAccounts() - .SetupAspNetCoreAndDatabase(options => - { - //Migrate individual account database - options.RegisterServiceToRunInJob>(); - //Add demo users to the database - options.RegisterServiceToRunInJob(); - }); - - - var app = builder.Build(); - - #region localization - - //see https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization#localization-middleware - app.UseRequestLocalization(localizationOptions); - //var options = app.Services.GetRequiredService>().Value; - //app.UseRequestLocalization(options); - - #endregion - - // 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(); - } - - app.UseHttpsRedirection(); - app.UseStaticFiles(); - - app.UseRouting(); - - app.UseAuthorization(); - - app.MapRazorPages(); - - app.Run(); - } - -} \ No newline at end of file + //Migrate individual account database + options.RegisterServiceToRunInJob>(); + //Add demo users to the database + options.RegisterServiceToRunInJob(); + }); + + +var app = builder.Build(); + +#region localization + +//see https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization#localization-middleware +app.UseRequestLocalization(localizationOptions); +//var options = app.Services.GetRequiredService>().Value; +//app.UseRequestLocalization(options); + +#endregion + +// 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(); +} + +app.UseHttpsRedirection(); +app.UseStaticFiles(); + +app.UseRouting(); + +app.UseAuthorization(); + +app.MapRazorPages(); + +app.Run();