From bc13da3e9f4ce609dc8cab7dba5166f7c4044349 Mon Sep 17 00:00:00 2001 From: maliming Date: Sat, 30 Dec 2023 17:54:45 +0800 Subject: [PATCH] Provide a way for the controller to skip all interceptors and filters. Resolve #18635 --- .../MultiTenancy/MultiTenancyMiddleware.cs | 5 ++-- .../AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs | 5 +++- .../Mvc/AbpMvcActionDescriptorProvider.cs | 30 +++++++++++++++++++ .../Mvc/Auditing/AbpAuditActionFilter.cs | 3 +- .../Mvc/Auditing/AbpAuditPageFilter.cs | 3 +- .../ExceptionHandling/AbpExceptionFilter.cs | 3 +- .../AbpExceptionPageFilter.cs | 3 +- .../Mvc/Features/AbpFeatureActionFilter.cs | 3 +- .../Mvc/Features/AbpFeaturePageFilter.cs | 3 +- .../GlobalFeatureActionFilter.cs | 3 +- .../GlobalFeatures/GlobalFeaturePageFilter.cs | 3 +- .../Mvc/Response/AbpNoContentActionFilter.cs | 3 +- .../AspNetCore/Mvc/Uow/AbpUowActionFilter.cs | 3 +- .../AspNetCore/Mvc/Uow/AbpUowPageFilter.cs | 3 +- .../Validation/AbpValidationActionFilter.cs | 3 +- .../Serilog/AbpSerilogMiddleware.cs | 5 ++-- .../AbpRequestLocalizationMiddleware.cs | 5 ++-- .../Auditing/AbpAuditingMiddleware.cs | 7 +++-- .../AbpExceptionHandlingMiddleware.cs | 5 ++-- .../Filters/DisableAbpFilterAttribute.cs | 19 ++++++++++++ .../Volo/Abp/AspNetCore/Filters/IAbpFilter.cs | 6 ++++ .../Middleware/AbpMiddlewareBase.cs | 20 +++++++++++++ .../Security/AbpSecurityHeadersMiddleware.cs | 5 ++-- .../Security/Claims/AbpClaimsMapMiddleware.cs | 5 ++-- .../Claims/AbpDynamicClaimsMiddleware.cs | 5 ++-- .../Tracing/AbpCorrelationIdMiddleware.cs | 5 ++-- .../AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs | 7 +++-- .../AbpRegistrationBuilderExtensions.cs | 4 ++- .../DisableInterceptorAttribute.cs | 9 ++++++ .../Mvc/FakeAuthenticationMiddleware.cs | 5 ++-- 30 files changed, 150 insertions(+), 38 deletions(-) create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs create mode 100644 framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs create mode 100644 framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs index 310add4bfc..520e2944f6 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; @@ -15,7 +16,7 @@ using Volo.Abp.Settings; namespace Volo.Abp.AspNetCore.MultiTenancy; -public class MultiTenancyMiddleware : IMiddleware, ITransientDependency +public class MultiTenancyMiddleware : AbpMiddlewareBase, ITransientDependency { public ILogger Logger { get; set; } @@ -38,7 +39,7 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency _options = options.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { TenantConfiguration? tenant = null; try diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs index c9b5984843..a2f2c1c989 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAspNetCoreMvcModule.cs @@ -14,6 +14,7 @@ using System.Linq; using System.Net; using System.Reflection; using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.DataAnnotations; using Microsoft.AspNetCore.Mvc.RazorPages; @@ -25,6 +26,7 @@ using Volo.Abp.ApiVersioning; using Volo.Abp.Application; using Volo.Abp.AspNetCore.Mvc.AntiForgery; using Volo.Abp.AspNetCore.Mvc.ApiExploring; +using Volo.Abp.AspNetCore.Mvc.ApplicationModels; using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.DataAnnotations; using Volo.Abp.AspNetCore.Mvc.DependencyInjection; @@ -176,6 +178,7 @@ public class AbpAspNetCoreMvcModule : AbpModule context.Services.Replace(ServiceDescriptor.Singleton()); context.Services.AddSingleton(); + context.Services.TryAddEnumerable(ServiceDescriptor.Transient()); context.Services.AddOptions() .Configure((mvcOptions, serviceProvider) => { @@ -247,7 +250,7 @@ public class AbpAspNetCoreMvcModule : AbpModule .Distinct(); AddToApplicationParts(partManager, controllerAssemblies); - + var additionalAssemblies = moduleContainer .Modules .SelectMany(m => m.GetAdditionalAssemblies()) diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs new file mode 100644 index 0000000000..9131cd44d6 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpMvcActionDescriptorProvider.cs @@ -0,0 +1,30 @@ +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Abstractions; +using Microsoft.AspNetCore.Mvc.Controllers; +using Volo.Abp.AspNetCore.Filters; + +namespace Volo.Abp.AspNetCore.Mvc.ApplicationModels; + +public class AbpMvcActionDescriptorProvider : IActionDescriptorProvider +{ + public virtual int Order => -1000 + 10; + + public virtual void OnProvidersExecuting(ActionDescriptorProviderContext context) + { + } + + public virtual void OnProvidersExecuted(ActionDescriptorProviderContext context) + { + foreach (var action in context.Results.Where(x => x is ControllerActionDescriptor).Cast()) + { + var disableFilterAttribute = action.ControllerTypeInfo.GetCustomAttribute(true); + if (disableFilterAttribute != null) + { + action.FilterDescriptors.RemoveAll(x => x.Filter is ServiceFilterAttribute serviceFilterAttribute && typeof(IAbpFilter).IsAssignableFrom(serviceFilterAttribute.ServiceType)); + } + } + } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs index 07101da0cb..d982adb12a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditActionFilter.cs @@ -5,12 +5,13 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Auditing; -public class AbpAuditActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpAuditActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs index 8445be60ae..6b97ac980a 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Auditing/AbpAuditPageFilter.cs @@ -5,12 +5,13 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Auditing; -public class AbpAuditPageFilter : IAsyncPageFilter, ITransientDependency +public class AbpAuditPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs index 61e37104ee..a63e557198 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionFilter.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; using Volo.Abp.ExceptionHandling; @@ -18,7 +19,7 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling; -public class AbpExceptionFilter : IAsyncExceptionFilter, ITransientDependency +public class AbpExceptionFilter : IAsyncExceptionFilter, IAbpFilter, ITransientDependency { public virtual async Task OnExceptionAsync(ExceptionContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs index 31784cb6ea..97c21268fe 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpExceptionPageFilter.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.ExceptionHandling; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; using Volo.Abp.ExceptionHandling; @@ -18,7 +19,7 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling; -public class AbpExceptionPageFilter : IAsyncPageFilter, ITransientDependency +public class AbpExceptionPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs index b4b70a5e1a..35bb9ba572 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeatureActionFilter.cs @@ -2,12 +2,13 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Features; namespace Volo.Abp.AspNetCore.Mvc.Features; -public class AbpFeatureActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpFeatureActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs index 3c586f762b..a35764c43c 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Features/AbpFeaturePageFilter.cs @@ -2,12 +2,13 @@ using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Features; namespace Volo.Abp.AspNetCore.Mvc.Features; -public class AbpFeaturePageFilter : IAsyncPageFilter, ITransientDependency +public class AbpFeaturePageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs index aae85e4d25..34f125f228 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeatureActionFilter.cs @@ -6,12 +6,13 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.GlobalFeatures; namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures; -public class GlobalFeatureActionFilter : IAsyncActionFilter, ITransientDependency +public class GlobalFeatureActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs index 46455730a4..3c43301221 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/GlobalFeatures/GlobalFeaturePageFilter.cs @@ -6,12 +6,13 @@ using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Aspects; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.GlobalFeatures; namespace Volo.Abp.AspNetCore.Mvc.GlobalFeatures; -public class GlobalFeaturePageFilter : IAsyncPageFilter, ITransientDependency +public class GlobalFeaturePageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs index b0565cbc4d..455dd52f24 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Response/AbpNoContentActionFilter.cs @@ -2,11 +2,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc.Response; -public class AbpNoContentActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpNoContentActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs index 14032b52d5..4a6a04a554 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowActionFilter.cs @@ -4,12 +4,13 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.Uow; -public class AbpUowActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpUowActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs index 7cfc1f0492..f4a7be3333 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Uow/AbpUowPageFilter.cs @@ -4,13 +4,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.AspNetCore.Uow; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Mvc.Uow; -public class AbpUowPageFilter : IAsyncPageFilter, ITransientDependency +public class AbpUowPageFilter : IAsyncPageFilter, IAbpFilter, ITransientDependency { public Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context) { diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs index e88cee4eb6..f866c85585 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Validation/AbpValidationActionFilter.cs @@ -3,13 +3,14 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Filters; using Volo.Abp.DependencyInjection; using Volo.Abp.Reflection; using Volo.Abp.Validation; namespace Volo.Abp.AspNetCore.Mvc.Validation; -public class AbpValidationActionFilter : IAsyncActionFilter, ITransientDependency +public class AbpValidationActionFilter : IAsyncActionFilter, IAbpFilter, ITransientDependency { public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { diff --git a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs index 1df327b742..52ee2e6b76 100644 --- a/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore.Serilog/Volo/Abp/AspNetCore/Serilog/AbpSerilogMiddleware.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Options; using Serilog.Context; using Serilog.Core; using Serilog.Core.Enrichers; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.Clients; using Volo.Abp.DependencyInjection; using Volo.Abp.MultiTenancy; @@ -13,7 +14,7 @@ using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Serilog; -public class AbpSerilogMiddleware : IMiddleware, ITransientDependency +public class AbpSerilogMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly ICurrentClient _currentClient; private readonly ICurrentTenant _currentTenant; @@ -35,7 +36,7 @@ public class AbpSerilogMiddleware : IMiddleware, ITransientDependency _options = options.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var enrichers = new List(); diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs index 6fd02a0470..cb531762ba 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/AspNetCore/RequestLocalization/AbpRequestLocalizationMiddleware.cs @@ -4,11 +4,12 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; namespace Microsoft.AspNetCore.RequestLocalization; -public class AbpRequestLocalizationMiddleware : IMiddleware, ITransientDependency +public class AbpRequestLocalizationMiddleware : AbpMiddlewareBase, ITransientDependency { public const string HttpContextItemName = "__AbpSetCultureCookie"; @@ -23,7 +24,7 @@ public class AbpRequestLocalizationMiddleware : IMiddleware, ITransientDependenc _loggerFactory = loggerFactory; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var middleware = new RequestLocalizationMiddleware( next, diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs index 0987a8fd8f..86cc997ae7 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Auditing/AbpAuditingMiddleware.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.Auditing; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; @@ -11,7 +12,7 @@ using Volo.Abp.Users; namespace Volo.Abp.AspNetCore.Auditing; -public class AbpAuditingMiddleware : IMiddleware, ITransientDependency +public class AbpAuditingMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly IAuditingManager _auditingManager; protected AbpAuditingOptions AuditingOptions { get; } @@ -34,9 +35,9 @@ public class AbpAuditingMiddleware : IMiddleware, ITransientDependency AspNetCoreAuditingOptions = aspNetCoreAuditingOptions.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { - if (!AuditingOptions.IsEnabled || IsIgnoredUrl(context)) + if (await ShouldSkipAsync(context, next) || !AuditingOptions.IsEnabled || IsIgnoredUrl(context)) { await next(context); return; diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs index b9d3b83068..7d5ef610e5 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpExceptionHandlingMiddleware.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.Net.Http.Headers; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; @@ -14,7 +15,7 @@ using Volo.Abp.Json; namespace Volo.Abp.AspNetCore.ExceptionHandling; -public class AbpExceptionHandlingMiddleware : IMiddleware, ITransientDependency +public class AbpExceptionHandlingMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly ILogger _logger; @@ -27,7 +28,7 @@ public class AbpExceptionHandlingMiddleware : IMiddleware, ITransientDependency _clearCacheHeadersDelegate = ClearCacheHeaders; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { try { diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs new file mode 100644 index 0000000000..035c7b288f --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/DisableAbpFilterAttribute.cs @@ -0,0 +1,19 @@ +using System; + +namespace Volo.Abp.AspNetCore.Filters; + +[AttributeUsage(AttributeTargets.Class)] +public class DisableAbpFilterAttribute : Attribute +{ + public bool SkipInMiddleware { get; set; } + + public DisableAbpFilterAttribute() + { + SkipInMiddleware = true; + } + + public DisableAbpFilterAttribute(bool skipInMiddleware) + { + SkipInMiddleware = skipInMiddleware; + } +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs new file mode 100644 index 0000000000..dbd55a8ed4 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Filters/IAbpFilter.cs @@ -0,0 +1,6 @@ +namespace Volo.Abp.AspNetCore.Filters; + +public interface IAbpFilter +{ + +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs new file mode 100644 index 0000000000..bbf79ba22e --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Middleware/AbpMiddlewareBase.cs @@ -0,0 +1,20 @@ +using System.Reflection; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc.Controllers; +using Volo.Abp.AspNetCore.Filters; + +namespace Volo.Abp.AspNetCore.Middleware; + +public abstract class AbpMiddlewareBase : IMiddleware +{ + protected Task ShouldSkipAsync(HttpContext context, RequestDelegate next) + { + var endpoint = context.GetEndpoint(); + var controllerActionDescriptor = endpoint?.Metadata.GetMetadata(); + var disableAbpFilterAttribute = controllerActionDescriptor?.ControllerTypeInfo.GetCustomAttribute(); + return Task.FromResult(disableAbpFilterAttribute != null && disableAbpFilterAttribute.SkipInMiddleware); + } + + public abstract Task InvokeAsync(HttpContext context, RequestDelegate next); +} diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs index ae2e3b173b..4542dd32e1 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/AbpSecurityHeadersMiddleware.cs @@ -5,11 +5,12 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using Microsoft.Extensions.Primitives; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Security; -public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency +public class AbpSecurityHeadersMiddleware : AbpMiddlewareBase, ITransientDependency { public IOptions Options { get; set; } protected const string ScriptSrcKey = "script-src"; @@ -20,7 +21,7 @@ public class AbpSecurityHeadersMiddleware : IMiddleware, ITransientDependency Options = options; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { /*X-Content-Type-Options header tells the browser to not try and “guess” what a mimetype of a resource might be, and to just take what mimetype the server has returned as fact.*/ AddHeader(context, "X-Content-Type-Options", "nosniff"); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs index ab23bdb944..bb2bedf901 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpClaimsMapMiddleware.cs @@ -4,14 +4,15 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Security.Claims; namespace Volo.Abp.AspNetCore.Security.Claims; -public class AbpClaimsMapMiddleware : IMiddleware, ITransientDependency +public class AbpClaimsMapMiddleware : AbpMiddlewareBase, ITransientDependency { - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var currentPrincipalAccessor = context.RequestServices .GetRequiredService(); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs index 777ca7744a..9c62fa8b25 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Security/Claims/AbpDynamicClaimsMiddleware.cs @@ -2,14 +2,15 @@ using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Security.Claims; namespace Volo.Abp.AspNetCore.Security.Claims; -public class AbpDynamicClaimsMiddleware : IMiddleware, ITransientDependency +public class AbpDynamicClaimsMiddleware : AbpMiddlewareBase, ITransientDependency { - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { if (context.User.Identity?.IsAuthenticated == true) { diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs index a5f954c260..38356f8151 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Tracing/AbpCorrelationIdMiddleware.cs @@ -3,12 +3,13 @@ using System.Collections.Generic; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; using System.Threading.Tasks; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Tracing; namespace Volo.Abp.AspNetCore.Tracing; -public class AbpCorrelationIdMiddleware : IMiddleware, ITransientDependency +public class AbpCorrelationIdMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly AbpCorrelationIdOptions _options; private readonly ICorrelationIdProvider _correlationIdProvider; @@ -20,7 +21,7 @@ public class AbpCorrelationIdMiddleware : IMiddleware, ITransientDependency _correlationIdProvider = correlationIdProvider; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { var correlationId = GetCorrelationIdFromRequest(context); using (_correlationIdProvider.Change(correlationId)) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs index 186501b4ac..013b3dad98 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/Uow/AbpUnitOfWorkMiddleware.cs @@ -3,12 +3,13 @@ using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.Options; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; using Volo.Abp.Uow; namespace Volo.Abp.AspNetCore.Uow; -public class AbpUnitOfWorkMiddleware : IMiddleware, ITransientDependency +public class AbpUnitOfWorkMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly IUnitOfWorkManager _unitOfWorkManager; private readonly AbpAspNetCoreUnitOfWorkOptions _options; @@ -21,9 +22,9 @@ public class AbpUnitOfWorkMiddleware : IMiddleware, ITransientDependency _options = options.Value; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { - if (IsIgnoredUrl(context)) + if (await ShouldSkipAsync(context, next) || IsIgnoredUrl(context)) { await next(context); return; diff --git a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs index 043f824b1a..9a2b1fa43a 100644 --- a/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs +++ b/framework/src/Volo.Abp.Autofac/Autofac/Builder/AbpRegistrationBuilderExtensions.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp.Autofac; using Volo.Abp.Castle.DynamicProxy; using Volo.Abp.DependencyInjection; +using Volo.Abp.DynamicProxy; using Volo.Abp.Modularity; namespace Autofac.Builder; @@ -73,7 +74,8 @@ public static class AbpRegistrationBuilderExtensions registrationAction.Invoke(serviceRegistredArgs); } - if (serviceRegistredArgs.Interceptors.Any()) + if (serviceRegistredArgs.Interceptors.Any() && + !serviceRegistredArgs.ImplementationType.IsDefined(typeof(DisableInterceptorAttribute), true)) { registrationBuilder = registrationBuilder.AddInterceptors( registrationActionList, diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs new file mode 100644 index 0000000000..7d3bf5bcc5 --- /dev/null +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DynamicProxy/DisableInterceptorAttribute.cs @@ -0,0 +1,9 @@ +using System; + +namespace Volo.Abp.DynamicProxy; + +[AttributeUsage(AttributeTargets.Class)] +public class DisableInterceptorAttribute : Attribute +{ + +} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs index 41a9b56fe5..a3cd4a57e6 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/FakeAuthenticationMiddleware.cs @@ -3,11 +3,12 @@ using System.Linq; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNetCore.Http; +using Volo.Abp.AspNetCore.Middleware; using Volo.Abp.DependencyInjection; namespace Volo.Abp.AspNetCore.Mvc; -public class FakeAuthenticationMiddleware : IMiddleware, ITransientDependency +public class FakeAuthenticationMiddleware : AbpMiddlewareBase, ITransientDependency { private readonly FakeUserClaims _fakeUserClaims; @@ -16,7 +17,7 @@ public class FakeAuthenticationMiddleware : IMiddleware, ITransientDependency _fakeUserClaims = fakeUserClaims; } - public async Task InvokeAsync(HttpContext context, RequestDelegate next) + public async override Task InvokeAsync(HttpContext context, RequestDelegate next) { if (_fakeUserClaims.Claims.Any()) {