From 2a7ee83a50446c1e6e8dbe98b62d46b8a050943c Mon Sep 17 00:00:00 2001 From: maliming Date: Fri, 12 Aug 2022 10:43:12 +0800 Subject: [PATCH] Introduce `AddIdentityServerCookieAuthentication` to `AbpIdentityServerBuilderOptions`. --- .../AbpIdentityServerBuilderOptions.cs | 6 +++ .../AbpIdentityServerDomainModule.cs | 40 +++++++++++++++---- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs index 1927c21746..66a15fd051 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerBuilderOptions.cs @@ -27,4 +27,10 @@ public class AbpIdentityServerBuilderOptions /// Set false to suppress AddDeveloperSigningCredential() call on the IIdentityServerBuilder. /// public bool AddDeveloperSigningCredential { get; set; } = true; + + /// + /// Adds the default cookie handlers and corresponding configuration + /// Default: true, Set false to suppress AddCookieAuthentication() call on the IIdentityServerBuilder. + /// + public bool AddIdentityServerCookieAuthentication { get; set; } = true; } diff --git a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs index 96ab4abd89..9b9dde5c96 100644 --- a/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs +++ b/modules/identityserver/src/Volo.Abp.IdentityServer.Domain/Volo/Abp/IdentityServer/AbpIdentityServerDomainModule.cs @@ -1,4 +1,5 @@ using System.Threading.Tasks; +using IdentityServer4.Configuration; using IdentityServer4.Services; using IdentityServer4.Stores; using Microsoft.Extensions.DependencyInjection; @@ -71,13 +72,7 @@ public class AbpIdentityServerDomainModule : AbpModule var configuration = services.GetConfiguration(); var builderOptions = services.ExecutePreConfiguredActions(); - var identityServerBuilder = services.AddIdentityServer(options => - { - options.Events.RaiseErrorEvents = true; - options.Events.RaiseInformationEvents = true; - options.Events.RaiseFailureEvents = true; - options.Events.RaiseSuccessEvents = true; - }); + var identityServerBuilder = AddIdentityServer(services, builderOptions); if (builderOptions.AddDeveloperSigningCredential) { @@ -110,6 +105,37 @@ public class AbpIdentityServerDomainModule : AbpModule } } + private static IIdentityServerBuilder AddIdentityServer(IServiceCollection services, AbpIdentityServerBuilderOptions abpIdentityServerBuilderOptions) + { + services.Configure(options => + { + options.Events.RaiseErrorEvents = true; + options.Events.RaiseInformationEvents = true; + options.Events.RaiseFailureEvents = true; + options.Events.RaiseSuccessEvents = true; + }); + + var identityServerBuilder = services.AddIdentityServerBuilder() + .AddRequiredPlatformServices() + .AddCoreServices() + .AddDefaultEndpoints() + .AddPluggableServices() + .AddValidators() + .AddResponseGenerators() + .AddDefaultSecretParsers() + .AddDefaultSecretValidators(); + + if (abpIdentityServerBuilderOptions.AddIdentityServerCookieAuthentication) + { + identityServerBuilder.AddCookieAuthentication(); + } + + // provide default in-memory implementation, not suitable for most production scenarios + identityServerBuilder.AddInMemoryPersistedGrants(); + + return identityServerBuilder; + } + public override void PostConfigureServices(ServiceConfigurationContext context) { OneTimeRunner.Run(() =>