From 380971264d6eb378b1bb45275d583d045e33f2d1 Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 1 Sep 2021 14:11:18 +0800 Subject: [PATCH] Refactor DefaultAbpAuthorizationExceptionHandler. --- ...DefaultAbpAuthorizationExceptionHandler.cs | 29 ++++--------------- 1 file changed, 6 insertions(+), 23 deletions(-) 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 8e86878294..de2a059715 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 @@ -18,33 +18,16 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling var handlerOptions = httpContext.RequestServices.GetRequiredService>().Value; if (handlerOptions.UseAuthenticationScheme) { - var handlers = httpContext.RequestServices.GetRequiredService(); - - if (!handlerOptions.AuthenticationScheme.IsNullOrWhiteSpace()) - { - var handler = await handlers.GetHandlerAsync(httpContext, handlerOptions.AuthenticationScheme); - if (handler != null) - { - if (isAuthenticated) - { - await handler.ForbidAsync(null); - } - else - { - await handler.ChallengeAsync(null); - } - - return true; - } - } - var authenticationSchemeProvider = httpContext.RequestServices.GetRequiredService(); - var scheme = isAuthenticated - ? await authenticationSchemeProvider.GetDefaultForbidSchemeAsync() - : await authenticationSchemeProvider.GetDefaultChallengeSchemeAsync(); + var scheme = !handlerOptions.AuthenticationScheme.IsNullOrWhiteSpace() + ? await authenticationSchemeProvider.GetSchemeAsync(handlerOptions.AuthenticationScheme) + : isAuthenticated + ? await authenticationSchemeProvider.GetDefaultForbidSchemeAsync() + : await authenticationSchemeProvider.GetDefaultChallengeSchemeAsync(); if (scheme != null) { + var handlers = httpContext.RequestServices.GetRequiredService(); var handler = await handlers.GetHandlerAsync(httpContext, scheme.Name); if (handler != null) {