From a112586110037c0bf5eb97e0edab28ccae27fc84 Mon Sep 17 00:00:00 2001 From: maliming Date: Tue, 7 Sep 2021 13:39:36 +0800 Subject: [PATCH] Use authentication scheme by default. --- .../ExceptionHandling/AbpExceptionFilter.cs | 22 +++--- .../AbpExceptionPageFilter.cs | 22 +++--- ...AbpAuthorizationExceptionHandlerOptions.cs | 7 -- .../AbpExceptionHandlingMiddleware.cs | 43 ++++++----- ...DefaultAbpAuthorizationExceptionHandler.cs | 75 +++++++++---------- .../IAbpAuthorizationExceptionHandler.cs | 2 +- ...horizationExceptionTestController_Tests.cs | 1 - ...AbpAuthorizationExceptionTestPage_Tests.cs | 1 - 8 files changed, 76 insertions(+), 97 deletions(-) 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 b1ce1dda92..9631e94da4 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 @@ -77,20 +77,18 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling if (context.Exception is AbpAuthorizationException) { - if (await context.HttpContext.RequestServices.GetRequiredService() - .HandleAsync(context.Exception.As(), context.HttpContext)) - { - context.Exception = null; //Handled! - return; - } + await context.HttpContext.RequestServices.GetRequiredService() + .HandleAsync(context.Exception.As(), context.HttpContext); } + else + { + context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); + context.HttpContext.Response.StatusCode = (int) context + .GetRequiredService() + .GetStatusCode(context.HttpContext, context.Exception); - context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); - context.HttpContext.Response.StatusCode = (int) context - .GetRequiredService() - .GetStatusCode(context.HttpContext, context.Exception); - - context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); + context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); + } context.Exception = null; //Handled! } 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 e6ebc235d1..2a8c136fbc 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 @@ -88,20 +88,18 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling if (context.Exception is AbpAuthorizationException) { - if (await context.HttpContext.RequestServices.GetRequiredService() - .HandleAsync(context.Exception.As(), context.HttpContext)) - { - context.Exception = null; //Handled! - return; - } + await context.HttpContext.RequestServices.GetRequiredService() + .HandleAsync(context.Exception.As(), context.HttpContext); } + else + { + context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); + context.HttpContext.Response.StatusCode = (int) context + .GetRequiredService() + .GetStatusCode(context.HttpContext, context.Exception); - context.HttpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); - context.HttpContext.Response.StatusCode = (int) context - .GetRequiredService() - .GetStatusCode(context.HttpContext, context.Exception); - - context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); + context.Result = new ObjectResult(new RemoteServiceErrorResponse(remoteServiceErrorInfo)); + } context.Exception = null; //Handled! } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpAuthorizationExceptionHandlerOptions.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpAuthorizationExceptionHandlerOptions.cs index 2be86dbebe..7c21e2ea63 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpAuthorizationExceptionHandlerOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/AbpAuthorizationExceptionHandlerOptions.cs @@ -2,13 +2,6 @@ { public class AbpAuthorizationExceptionHandlerOptions { - public bool UseAuthenticationScheme { get; set; } - public string AuthenticationScheme { get; set; } - - public AbpAuthorizationExceptionHandlerOptions() - { - UseAuthenticationScheme = true; - } } } 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 b4aa9745b5..51c78b4a83 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 @@ -68,30 +68,29 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling if (exception is AbpAuthorizationException) { - if (await httpContext.RequestServices.GetRequiredService() - .HandleAsync(exception.As(), httpContext)) - { - return; - } + await httpContext.RequestServices.GetRequiredService() + .HandleAsync(exception.As(), httpContext); } - - var errorInfoConverter = httpContext.RequestServices.GetRequiredService(); - var statusCodeFinder = httpContext.RequestServices.GetRequiredService(); - var jsonSerializer = httpContext.RequestServices.GetRequiredService(); - var options = httpContext.RequestServices.GetRequiredService>().Value; - - httpContext.Response.Clear(); - httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception); - httpContext.Response.OnStarting(_clearCacheHeadersDelegate, httpContext.Response); - httpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); - - await httpContext.Response.WriteAsync( - jsonSerializer.Serialize( - new RemoteServiceErrorResponse( - errorInfoConverter.Convert(exception, options.SendExceptionsDetailsToClients) + else + { + var errorInfoConverter = httpContext.RequestServices.GetRequiredService(); + var statusCodeFinder = httpContext.RequestServices.GetRequiredService(); + var jsonSerializer = httpContext.RequestServices.GetRequiredService(); + var options = httpContext.RequestServices.GetRequiredService>().Value; + + httpContext.Response.Clear(); + httpContext.Response.StatusCode = (int)statusCodeFinder.GetStatusCode(httpContext, exception); + httpContext.Response.OnStarting(_clearCacheHeadersDelegate, httpContext.Response); + httpContext.Response.Headers.Add(AbpHttpConsts.AbpErrorFormat, "true"); + + await httpContext.Response.WriteAsync( + jsonSerializer.Serialize( + new RemoteServiceErrorResponse( + errorInfoConverter.Convert(exception, options.SendExceptionsDetailsToClients) + ) ) - ) - ); + ); + } } private Task ClearCacheHeaders(object state) diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultAbpAuthorizationExceptionHandler.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultAbpAuthorizationExceptionHandler.cs index 685c2a73bd..87bc653092 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultAbpAuthorizationExceptionHandler.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/DefaultAbpAuthorizationExceptionHandler.cs @@ -11,64 +11,57 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling { public class DefaultAbpAuthorizationExceptionHandler : IAbpAuthorizationExceptionHandler, ITransientDependency { - public virtual async Task HandleAsync(AbpAuthorizationException exception, HttpContext httpContext) + public virtual async Task HandleAsync(AbpAuthorizationException exception, HttpContext httpContext) { var handlerOptions = httpContext.RequestServices.GetRequiredService>().Value; - if (handlerOptions.UseAuthenticationScheme) - { - var isAuthenticated = httpContext.User.Identity?.IsAuthenticated ?? false; - var authenticationSchemeProvider = httpContext.RequestServices.GetRequiredService(); + var isAuthenticated = httpContext.User.Identity?.IsAuthenticated ?? false; + var authenticationSchemeProvider = httpContext.RequestServices.GetRequiredService(); - AuthenticationScheme scheme = null; + AuthenticationScheme scheme = null; - if (!handlerOptions.AuthenticationScheme.IsNullOrWhiteSpace()) + if (!handlerOptions.AuthenticationScheme.IsNullOrWhiteSpace()) + { + scheme = await authenticationSchemeProvider.GetSchemeAsync(handlerOptions.AuthenticationScheme); + if (scheme == null) + { + throw new AbpException($"No authentication scheme named {handlerOptions.AuthenticationScheme} was found."); + } + } + else + { + if (isAuthenticated) { - scheme = await authenticationSchemeProvider.GetSchemeAsync(handlerOptions.AuthenticationScheme); + scheme = await authenticationSchemeProvider.GetDefaultForbidSchemeAsync(); if (scheme == null) { - throw new AbpException($"No authentication scheme named {handlerOptions.AuthenticationScheme} was found."); + throw new AbpException($"There was no DefaultForbidScheme found."); } } else { - if (isAuthenticated) - { - scheme = await authenticationSchemeProvider.GetDefaultForbidSchemeAsync(); - if (scheme == null) - { - throw new AbpException($"There was no DefaultForbidScheme found."); - } - } - else + scheme = await authenticationSchemeProvider.GetDefaultChallengeSchemeAsync(); + if (scheme == null) { - scheme = await authenticationSchemeProvider.GetDefaultChallengeSchemeAsync(); - if (scheme == null) - { - throw new AbpException($"There was no DefaultChallengeScheme found."); - } + throw new AbpException($"There was no DefaultChallengeScheme found."); } } + } - var handlers = httpContext.RequestServices.GetRequiredService(); - var handler = await handlers.GetHandlerAsync(httpContext, scheme.Name); - if (handler == null) - { - throw new AbpException($"No handler of {scheme.Name} was found."); - } - - if (isAuthenticated) - { - await handler.ForbidAsync(null); - } - else - { - await handler.ChallengeAsync(null); - } - - return true; + var handlers = httpContext.RequestServices.GetRequiredService(); + var handler = await handlers.GetHandlerAsync(httpContext, scheme.Name); + if (handler == null) + { + throw new AbpException($"No handler of {scheme.Name} was found."); } - return false; + if (isAuthenticated) + { + await handler.ForbidAsync(null); + } + else + { + await handler.ChallengeAsync(null); + } } } } diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/IAbpAuthorizationExceptionHandler.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/IAbpAuthorizationExceptionHandler.cs index 9f95c18f28..efcbf9c7ea 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/IAbpAuthorizationExceptionHandler.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/ExceptionHandling/IAbpAuthorizationExceptionHandler.cs @@ -6,6 +6,6 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling { public interface IAbpAuthorizationExceptionHandler { - Task HandleAsync(AbpAuthorizationException exception, HttpContext httpContext); + Task HandleAsync(AbpAuthorizationException exception, HttpContext httpContext); } } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestController_Tests.cs index 6dd4a97d7c..aa0e6b5557 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestController_Tests.cs @@ -34,7 +34,6 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling services.Configure(options => { - options.UseAuthenticationScheme = true; options.AuthenticationScheme = "Cookie"; }); } diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestPage_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestPage_Tests.cs index f3bf1cca6d..6bc37c19e4 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestPage_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ExceptionHandling/AbpAuthorizationExceptionTestPage_Tests.cs @@ -34,7 +34,6 @@ namespace Volo.Abp.AspNetCore.Mvc.ExceptionHandling services.Configure(options => { - options.UseAuthenticationScheme = true; options.AuthenticationScheme = "Cookie"; }); }