From 4bfcd720ff77c55746a9b0e889aa6cf2f19846f4 Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 1 Feb 2024 13:27:58 +0800 Subject: [PATCH] Add `AuthenticationOptions`. --- ...reComponentsWebAssemblyBasicThemeModule.cs | 13 +++++++++- .../AuthenticationOptions.cs | 8 +++++++ .../Themes/Basic/LoginDisplay.razor | 4 +++- .../Themes/Basic/LoginDisplay.razor.cs | 4 +--- .../Basic/WebAssemblyRedirectToLogin.razor | 9 ++++--- .../Menus/MyProjectNameMenuContributor.cs | 6 ++--- .../wwwroot/appsettings.json | 3 --- .../Menus/MyProjectNameMenuContributor.cs | 6 ++--- .../MyProjectNameBlazorClientModule.cs | 24 +++++++++---------- .../wwwroot/appsettings.json | 8 ------- .../Menus/MyProjectNameMenuContributor.cs | 6 ++--- .../MyProjectNameBlazorClientModule.cs | 13 +++++++--- .../wwwroot/appsettings.json | 6 ----- 13 files changed, 56 insertions(+), 54 deletions(-) create mode 100644 modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AuthenticationOptions.cs diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AbpAspNetCoreComponentsWebAssemblyBasicThemeModule.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AbpAspNetCoreComponentsWebAssemblyBasicThemeModule.cs index a1b179abf3..eb38111b37 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AbpAspNetCoreComponentsWebAssemblyBasicThemeModule.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AbpAspNetCoreComponentsWebAssemblyBasicThemeModule.cs @@ -1,4 +1,6 @@ -using Volo.Abp.AspNetCore.Components.Web.BasicTheme; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.AspNetCore.Components.Server; +using Volo.Abp.AspNetCore.Components.Web.BasicTheme; using Volo.Abp.AspNetCore.Components.Web.Theming.Routing; using Volo.Abp.AspNetCore.Components.Web.Theming.Toolbars; using Volo.Abp.AspNetCore.Components.WebAssembly.Theming; @@ -25,5 +27,14 @@ public class AbpAspNetCoreComponentsWebAssemblyBasicThemeModule : AbpModule { options.Contributors.Add(new BasicThemeToolbarContributor()); }); + + if (context.Services.ExecutePreConfiguredActions().IsBlazorWebApp) + { + Configure(options => + { + options.LoginUrl = "Account/Login"; + options.LogoutUrl = "Account/Logout"; + }); + } } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AuthenticationOptions.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AuthenticationOptions.cs new file mode 100644 index 0000000000..aaa03fed6e --- /dev/null +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AuthenticationOptions.cs @@ -0,0 +1,8 @@ +namespace Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme; + +public class AuthenticationOptions +{ + public string LoginUrl { get; set; } = "authentication/login"; + + public string LogoutUrl { get; set; } = "authentication/logout"; +} diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor index 0715560b1c..99a2f70439 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor @@ -2,12 +2,14 @@ @using Volo.Abp.Users @using Volo.Abp.MultiTenancy @using global::Localization.Resources.AbpUi +@using Microsoft.Extensions.Options @inherits AbpComponentBase @inject ICurrentUser CurrentUser @inject ICurrentTenant CurrentTenant @inject IJSRuntime JsRuntime @inject NavigationManager Navigation @inject IStringLocalizer UiLocalizer +@inject IOptions AuthenticationOptions @@ -35,6 +37,6 @@ - @UiLocalizer["Login"] + @UiLocalizer["Login"] diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs index 4312369b2b..c3fa3a5bde 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs @@ -1,8 +1,6 @@ using System; using System.Threading.Tasks; -using JetBrains.Annotations; using Microsoft.AspNetCore.Components; -using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.JSInterop; @@ -61,6 +59,6 @@ public partial class LoginDisplay : IDisposable private void BeginSignOut() { - Navigation.NavigateToLogout("authentication/logout"); + Navigation.NavigateToLogout(AuthenticationOptions.Value.LogoutUrl); } } diff --git a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/WebAssemblyRedirectToLogin.razor b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/WebAssemblyRedirectToLogin.razor index ebae71d62a..5314d5fe78 100644 --- a/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/WebAssemblyRedirectToLogin.razor +++ b/modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/WebAssemblyRedirectToLogin.razor @@ -1,17 +1,16 @@ @inject NavigationManager Navigation - @using Volo.Abp.DependencyInjection @using Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic @using Microsoft.AspNetCore.Components.WebAssembly.Authentication +@using Microsoft.Extensions.Options @inherits RedirectToLogin @attribute [ExposeServices(typeof(RedirectToLogin))] @attribute [Dependency(ReplaceServices = true)] +@inject IOptions AuthenticationOptions @code { - protected override void OnInitialized() { - Navigation.NavigateToLogin("authentication/login"); + Navigation.NavigateToLogin(AuthenticationOptions.Value.LoginUrl); } - -} \ No newline at end of file +} diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/Menus/MyProjectNameMenuContributor.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/Menus/MyProjectNameMenuContributor.cs index 6a5225f708..049bb0f679 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/Menus/MyProjectNameMenuContributor.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/Menus/MyProjectNameMenuContributor.cs @@ -46,7 +46,7 @@ public class MyProjectNameMenuContributor : IMenuContributor icon: "fas fa-home" ) ); - + var administration = context.Menu.GetAdministration(); if (MultiTenancyConsts.IsEnabled) @@ -68,12 +68,10 @@ public class MyProjectNameMenuContributor : IMenuContributor { var accountStringLocalizer = context.GetLocalizer(); - var authServerUrl = _configuration["AuthServer:Authority"] ?? ""; - context.Menu.AddItem(new ApplicationMenuItem( "Account.Manage", accountStringLocalizer["MyAccount"], - $"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", + $"Account/Manage", icon: "fa fa-cog", order: 1000, null).RequireAuthenticated()); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/wwwroot/appsettings.json b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/wwwroot/appsettings.json index 6aa46a271c..b9158bbaaa 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/wwwroot/appsettings.json +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/wwwroot/appsettings.json @@ -1,7 +1,4 @@ { - "App": { - "SelfUrl": "https://localhost:44307" - }, "AuthServer": { "Authority": "https://localhost:44305", "ClientId": "MyProjectName_Blazor", diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/Menus/MyProjectNameMenuContributor.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/Menus/MyProjectNameMenuContributor.cs index 0b06c00477..62d46a5dbb 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/Menus/MyProjectNameMenuContributor.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/Menus/MyProjectNameMenuContributor.cs @@ -46,7 +46,7 @@ public class MyProjectNameMenuContributor : IMenuContributor icon: "fas fa-home" ) ); - + var administration = context.Menu.GetAdministration(); if (MultiTenancyConsts.IsEnabled) @@ -68,12 +68,10 @@ public class MyProjectNameMenuContributor : IMenuContributor { var accountStringLocalizer = context.GetLocalizer(); - var authServerUrl = _configuration["AuthServer:Authority"] ?? ""; - context.Menu.AddItem(new ApplicationMenuItem( "Account.Manage", accountStringLocalizer["MyAccount"], - $"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", + $"Account/Manage", icon: "fa fa-cog", order: 1000, null).RequireAuthenticated()); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/MyProjectNameBlazorClientModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/MyProjectNameBlazorClientModule.cs index 4ab2e40237..bdeb096207 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/MyProjectNameBlazorClientModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/MyProjectNameBlazorClientModule.cs @@ -3,12 +3,14 @@ using System.Net.Http; using Blazorise.Bootstrap5; using Blazorise.Icons.FontAwesome; using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client.Menus; using OpenIddict.Abstractions; +using Volo.Abp.AspNetCore.Components.Server; using Volo.Abp.AspNetCore.Components.Web.Theming.Routing; using Volo.Abp.AspNetCore.Components.WebAssembly.LeptonXLiteTheme; using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp; @@ -34,6 +36,14 @@ namespace MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client; )] public class MyProjectNameBlazorClientModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + PreConfigure(options => + { + options.IsBlazorWebApp = true; + }); + } + public override void ConfigureServices(ServiceConfigurationContext context) { var environment = context.Services.GetSingletonInstance(); @@ -73,19 +83,9 @@ public class MyProjectNameBlazorClientModule : AbpModule private static void ConfigureAuthentication(WebAssemblyHostBuilder builder) { + //TODO: Remove SignOutSessionStateManager in new version. + builder.Services.TryAddScoped(); builder.Services.AddBlazorWebAppServices(); - builder.Services.AddCascadingAuthenticationState(); - builder.Services.AddOidcAuthentication(options => - { - builder.Configuration.Bind("AuthServer", options.ProviderOptions); - options.UserOptions.NameClaim = OpenIddictConstants.Claims.Name; - options.UserOptions.RoleClaim = OpenIddictConstants.Claims.Role; - - options.ProviderOptions.DefaultScopes.Add("MyProjectName"); - options.ProviderOptions.DefaultScopes.Add("roles"); - options.ProviderOptions.DefaultScopes.Add("email"); - options.ProviderOptions.DefaultScopes.Add("phone"); - }); } private static void ConfigureHttpClient(ServiceConfigurationContext context, IWebAssemblyHostEnvironment environment) diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/wwwroot/appsettings.json b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/wwwroot/appsettings.json index 8680435bfe..26eaa4b1fb 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/wwwroot/appsettings.json +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/wwwroot/appsettings.json @@ -1,12 +1,4 @@ { - "App": { - "SelfUrl": "https://localhost:44307" - }, - "AuthServer": { - "Authority": "https://localhost:44308", - "ClientId": "MyProjectName_Blazor", - "ResponseType": "code" - }, "RemoteServices": { "Default": { "BaseUrl": "https://localhost:44308" diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/Menus/MyProjectNameMenuContributor.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/Menus/MyProjectNameMenuContributor.cs index da5b5aead1..1b36528ca4 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/Menus/MyProjectNameMenuContributor.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/Menus/MyProjectNameMenuContributor.cs @@ -46,7 +46,7 @@ public class MyProjectNameMenuContributor : IMenuContributor icon: "fas fa-home" ) ); - + var administration = context.Menu.GetAdministration(); if (MultiTenancyConsts.IsEnabled) @@ -68,12 +68,10 @@ public class MyProjectNameMenuContributor : IMenuContributor { var accountStringLocalizer = context.GetLocalizer(); - var authServerUrl = _configuration["AuthServer:Authority"] ?? ""; - context.Menu.AddItem(new ApplicationMenuItem( "Account.Manage", accountStringLocalizer["MyAccount"], - $"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", + $"Account/Manage", icon: "fa fa-cog", order: 1000, null).RequireAuthenticated()); diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/MyProjectNameBlazorClientModule.cs b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/MyProjectNameBlazorClientModule.cs index e19cd07d5d..1292868274 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/MyProjectNameBlazorClientModule.cs +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/MyProjectNameBlazorClientModule.cs @@ -2,18 +2,16 @@ using System.Net.Http; using Blazorise.Bootstrap5; using Blazorise.Icons.FontAwesome; -using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client.Menus; +using Volo.Abp.AspNetCore.Components.Server; using Volo.Abp.AspNetCore.Components.Web.Theming.Routing; using Volo.Abp.AspNetCore.Components.WebAssembly.LeptonXLiteTheme; -using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp; using Volo.Abp.Autofac.WebAssembly; using Volo.Abp.AutoMapper; -using Volo.Abp.Http.Client.Authentication; using Volo.Abp.Identity.Blazor.WebAssembly; using Volo.Abp.Modularity; using Volo.Abp.SettingManagement.Blazor.WebAssembly; @@ -32,6 +30,14 @@ namespace MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client; )] public class MyProjectNameBlazorClientModule : AbpModule { + public override void PreConfigureServices(ServiceConfigurationContext context) + { + PreConfigure(options => + { + options.IsBlazorWebApp = true; + }); + } + public override void ConfigureServices(ServiceConfigurationContext context) { var environment = context.Services.GetSingletonInstance(); @@ -70,6 +76,7 @@ public class MyProjectNameBlazorClientModule : AbpModule private static void ConfigureAuthentication(WebAssemblyHostBuilder builder) { + //TODO: Remove SignOutSessionStateManager in new version. builder.Services.TryAddScoped(); builder.Services.AddBlazorWebAppTieredServices(); } diff --git a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/wwwroot/appsettings.json b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/wwwroot/appsettings.json index 110d342e92..29a9fca4c3 100644 --- a/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/wwwroot/appsettings.json +++ b/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/wwwroot/appsettings.json @@ -1,10 +1,4 @@ { - "App": { - "SelfUrl": "https://localhost:44309" - }, - "AuthServer": { - "Authority": "https://localhost:44301" - }, "RemoteServices": { "Default": { "BaseUrl": "https://localhost:44300"