Browse Source

Add `AuthenticationOptions`.

pull/18876/head
maliming 3 years ago
parent
commit
4bfcd720ff
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 13
      modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AbpAspNetCoreComponentsWebAssemblyBasicThemeModule.cs
  2. 8
      modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/AuthenticationOptions.cs
  3. 4
      modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor
  4. 4
      modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs
  5. 9
      modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/WebAssemblyRedirectToLogin.razor
  6. 6
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/Menus/MyProjectNameMenuContributor.cs
  7. 3
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/wwwroot/appsettings.json
  8. 6
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/Menus/MyProjectNameMenuContributor.cs
  9. 24
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/MyProjectNameBlazorClientModule.cs
  10. 8
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client/wwwroot/appsettings.json
  11. 6
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/Menus/MyProjectNameMenuContributor.cs
  12. 13
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/MyProjectNameBlazorClientModule.cs
  13. 6
      templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/wwwroot/appsettings.json

13
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.Routing;
using Volo.Abp.AspNetCore.Components.Web.Theming.Toolbars; using Volo.Abp.AspNetCore.Components.Web.Theming.Toolbars;
using Volo.Abp.AspNetCore.Components.WebAssembly.Theming; using Volo.Abp.AspNetCore.Components.WebAssembly.Theming;
@ -25,5 +27,14 @@ public class AbpAspNetCoreComponentsWebAssemblyBasicThemeModule : AbpModule
{ {
options.Contributors.Add(new BasicThemeToolbarContributor()); options.Contributors.Add(new BasicThemeToolbarContributor());
}); });
if (context.Services.ExecutePreConfiguredActions<AbpAspNetCoreComponentsWebOptions>().IsBlazorWebApp)
{
Configure<AuthenticationOptions>(options =>
{
options.LoginUrl = "Account/Login";
options.LogoutUrl = "Account/Logout";
});
}
} }
} }

8
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";
}

4
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.Users
@using Volo.Abp.MultiTenancy @using Volo.Abp.MultiTenancy
@using global::Localization.Resources.AbpUi @using global::Localization.Resources.AbpUi
@using Microsoft.Extensions.Options
@inherits AbpComponentBase @inherits AbpComponentBase
@inject ICurrentUser CurrentUser @inject ICurrentUser CurrentUser
@inject ICurrentTenant CurrentTenant @inject ICurrentTenant CurrentTenant
@inject IJSRuntime JsRuntime @inject IJSRuntime JsRuntime
@inject NavigationManager Navigation @inject NavigationManager Navigation
@inject IStringLocalizer<AbpUiResource> UiLocalizer @inject IStringLocalizer<AbpUiResource> UiLocalizer
@inject IOptions<AuthenticationOptions> AuthenticationOptions
<AuthorizeView> <AuthorizeView>
<Authorized> <Authorized>
<Dropdown RightAligned="true"> <Dropdown RightAligned="true">
@ -35,6 +37,6 @@
</Dropdown> </Dropdown>
</Authorized> </Authorized>
<NotAuthorized> <NotAuthorized>
<a class="nav-link" href="authentication/login">@UiLocalizer["Login"]</a> <a class="nav-link" href="@AuthenticationOptions.Value.LoginUrl">@UiLocalizer["Login"]</a>
</NotAuthorized> </NotAuthorized>
</AuthorizeView> </AuthorizeView>

4
modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/LoginDisplay.razor.cs

@ -1,8 +1,6 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Routing; using Microsoft.AspNetCore.Components.Routing;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.JSInterop; using Microsoft.JSInterop;
@ -61,6 +59,6 @@ public partial class LoginDisplay : IDisposable
private void BeginSignOut() private void BeginSignOut()
{ {
Navigation.NavigateToLogout("authentication/logout"); Navigation.NavigateToLogout(AuthenticationOptions.Value.LogoutUrl);
} }
} }

9
modules/basic-theme/src/Volo.Abp.AspNetCore.Components.WebAssembly.BasicTheme/Themes/Basic/WebAssemblyRedirectToLogin.razor

@ -1,17 +1,16 @@
@inject NavigationManager Navigation @inject NavigationManager Navigation
@using Volo.Abp.DependencyInjection @using Volo.Abp.DependencyInjection
@using Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic @using Volo.Abp.AspNetCore.Components.Web.BasicTheme.Themes.Basic
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication @using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Microsoft.Extensions.Options
@inherits RedirectToLogin @inherits RedirectToLogin
@attribute [ExposeServices(typeof(RedirectToLogin))] @attribute [ExposeServices(typeof(RedirectToLogin))]
@attribute [Dependency(ReplaceServices = true)] @attribute [Dependency(ReplaceServices = true)]
@inject IOptions<AuthenticationOptions> AuthenticationOptions
@code { @code {
protected override void OnInitialized() protected override void OnInitialized()
{ {
Navigation.NavigateToLogin("authentication/login"); Navigation.NavigateToLogin(AuthenticationOptions.Value.LoginUrl);
} }
}
}

6
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/Menus/MyProjectNameMenuContributor.cs

@ -46,7 +46,7 @@ public class MyProjectNameMenuContributor : IMenuContributor
icon: "fas fa-home" icon: "fas fa-home"
) )
); );
var administration = context.Menu.GetAdministration(); var administration = context.Menu.GetAdministration();
if (MultiTenancyConsts.IsEnabled) if (MultiTenancyConsts.IsEnabled)
@ -68,12 +68,10 @@ public class MyProjectNameMenuContributor : IMenuContributor
{ {
var accountStringLocalizer = context.GetLocalizer<AccountResource>(); var accountStringLocalizer = context.GetLocalizer<AccountResource>();
var authServerUrl = _configuration["AuthServer:Authority"] ?? "";
context.Menu.AddItem(new ApplicationMenuItem( context.Menu.AddItem(new ApplicationMenuItem(
"Account.Manage", "Account.Manage",
accountStringLocalizer["MyAccount"], accountStringLocalizer["MyAccount"],
$"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", $"Account/Manage",
icon: "fa fa-cog", icon: "fa fa-cog",
order: 1000, order: 1000,
null).RequireAuthenticated()); null).RequireAuthenticated());

3
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Client/wwwroot/appsettings.json

@ -1,7 +1,4 @@
{ {
"App": {
"SelfUrl": "https://localhost:44307"
},
"AuthServer": { "AuthServer": {
"Authority": "https://localhost:44305", "Authority": "https://localhost:44305",
"ClientId": "MyProjectName_Blazor", "ClientId": "MyProjectName_Blazor",

6
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" icon: "fas fa-home"
) )
); );
var administration = context.Menu.GetAdministration(); var administration = context.Menu.GetAdministration();
if (MultiTenancyConsts.IsEnabled) if (MultiTenancyConsts.IsEnabled)
@ -68,12 +68,10 @@ public class MyProjectNameMenuContributor : IMenuContributor
{ {
var accountStringLocalizer = context.GetLocalizer<AccountResource>(); var accountStringLocalizer = context.GetLocalizer<AccountResource>();
var authServerUrl = _configuration["AuthServer:Authority"] ?? "";
context.Menu.AddItem(new ApplicationMenuItem( context.Menu.AddItem(new ApplicationMenuItem(
"Account.Manage", "Account.Manage",
accountStringLocalizer["MyAccount"], accountStringLocalizer["MyAccount"],
$"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", $"Account/Manage",
icon: "fa fa-cog", icon: "fa fa-cog",
order: 1000, order: 1000,
null).RequireAuthenticated()); null).RequireAuthenticated());

24
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.Bootstrap5;
using Blazorise.Icons.FontAwesome; using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client.Menus; using MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client.Menus;
using OpenIddict.Abstractions; using OpenIddict.Abstractions;
using Volo.Abp.AspNetCore.Components.Server;
using Volo.Abp.AspNetCore.Components.Web.Theming.Routing; using Volo.Abp.AspNetCore.Components.Web.Theming.Routing;
using Volo.Abp.AspNetCore.Components.WebAssembly.LeptonXLiteTheme; using Volo.Abp.AspNetCore.Components.WebAssembly.LeptonXLiteTheme;
using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp; using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp;
@ -34,6 +36,14 @@ namespace MyCompanyName.MyProjectName.Blazor.Server.WebApp.Client;
)] )]
public class MyProjectNameBlazorClientModule : AbpModule public class MyProjectNameBlazorClientModule : AbpModule
{ {
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpAspNetCoreComponentsWebOptions>(options =>
{
options.IsBlazorWebApp = true;
});
}
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>(); var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>();
@ -73,19 +83,9 @@ public class MyProjectNameBlazorClientModule : AbpModule
private static void ConfigureAuthentication(WebAssemblyHostBuilder builder) private static void ConfigureAuthentication(WebAssemblyHostBuilder builder)
{ {
//TODO: Remove SignOutSessionStateManager in new version.
builder.Services.TryAddScoped<SignOutSessionStateManager>();
builder.Services.AddBlazorWebAppServices(); 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) private static void ConfigureHttpClient(ServiceConfigurationContext context, IWebAssemblyHostEnvironment environment)

8
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": { "RemoteServices": {
"Default": { "Default": {
"BaseUrl": "https://localhost:44308" "BaseUrl": "https://localhost:44308"

6
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" icon: "fas fa-home"
) )
); );
var administration = context.Menu.GetAdministration(); var administration = context.Menu.GetAdministration();
if (MultiTenancyConsts.IsEnabled) if (MultiTenancyConsts.IsEnabled)
@ -68,12 +68,10 @@ public class MyProjectNameMenuContributor : IMenuContributor
{ {
var accountStringLocalizer = context.GetLocalizer<AccountResource>(); var accountStringLocalizer = context.GetLocalizer<AccountResource>();
var authServerUrl = _configuration["AuthServer:Authority"] ?? "";
context.Menu.AddItem(new ApplicationMenuItem( context.Menu.AddItem(new ApplicationMenuItem(
"Account.Manage", "Account.Manage",
accountStringLocalizer["MyAccount"], accountStringLocalizer["MyAccount"],
$"{authServerUrl.EnsureEndsWith('/')}Account/Manage?returnUrl={_configuration["App:SelfUrl"]}", $"Account/Manage",
icon: "fa fa-cog", icon: "fa fa-cog",
order: 1000, order: 1000,
null).RequireAuthenticated()); null).RequireAuthenticated());

13
templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client/MyProjectNameBlazorClientModule.cs

@ -2,18 +2,16 @@
using System.Net.Http; using System.Net.Http;
using Blazorise.Bootstrap5; using Blazorise.Bootstrap5;
using Blazorise.Icons.FontAwesome; using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.DependencyInjection.Extensions;
using MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client.Menus; 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.Web.Theming.Routing;
using Volo.Abp.AspNetCore.Components.WebAssembly.LeptonXLiteTheme; using Volo.Abp.AspNetCore.Components.WebAssembly.LeptonXLiteTheme;
using Volo.Abp.AspNetCore.Components.WebAssembly.WebApp;
using Volo.Abp.Autofac.WebAssembly; using Volo.Abp.Autofac.WebAssembly;
using Volo.Abp.AutoMapper; using Volo.Abp.AutoMapper;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Identity.Blazor.WebAssembly; using Volo.Abp.Identity.Blazor.WebAssembly;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
using Volo.Abp.SettingManagement.Blazor.WebAssembly; using Volo.Abp.SettingManagement.Blazor.WebAssembly;
@ -32,6 +30,14 @@ namespace MyCompanyName.MyProjectName.Blazor.Server.WebApp.Tiered.Client;
)] )]
public class MyProjectNameBlazorClientModule : AbpModule public class MyProjectNameBlazorClientModule : AbpModule
{ {
public override void PreConfigureServices(ServiceConfigurationContext context)
{
PreConfigure<AbpAspNetCoreComponentsWebOptions>(options =>
{
options.IsBlazorWebApp = true;
});
}
public override void ConfigureServices(ServiceConfigurationContext context) public override void ConfigureServices(ServiceConfigurationContext context)
{ {
var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>(); var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>();
@ -70,6 +76,7 @@ public class MyProjectNameBlazorClientModule : AbpModule
private static void ConfigureAuthentication(WebAssemblyHostBuilder builder) private static void ConfigureAuthentication(WebAssemblyHostBuilder builder)
{ {
//TODO: Remove SignOutSessionStateManager in new version.
builder.Services.TryAddScoped<SignOutSessionStateManager>(); builder.Services.TryAddScoped<SignOutSessionStateManager>();
builder.Services.AddBlazorWebAppTieredServices(); builder.Services.AddBlazorWebAppTieredServices();
} }

6
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": { "RemoteServices": {
"Default": { "Default": {
"BaseUrl": "https://localhost:44300" "BaseUrl": "https://localhost:44300"

Loading…
Cancel
Save