|
|
|
@ -11,64 +11,57 @@ namespace Volo.Abp.AspNetCore.ExceptionHandling |
|
|
|
{ |
|
|
|
public class DefaultAbpAuthorizationExceptionHandler : IAbpAuthorizationExceptionHandler, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<bool> HandleAsync(AbpAuthorizationException exception, HttpContext httpContext) |
|
|
|
public virtual async Task HandleAsync(AbpAuthorizationException exception, HttpContext httpContext) |
|
|
|
{ |
|
|
|
var handlerOptions = httpContext.RequestServices.GetRequiredService<IOptions<AbpAuthorizationExceptionHandlerOptions>>().Value; |
|
|
|
if (handlerOptions.UseAuthenticationScheme) |
|
|
|
{ |
|
|
|
var isAuthenticated = httpContext.User.Identity?.IsAuthenticated ?? false; |
|
|
|
var authenticationSchemeProvider = httpContext.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>(); |
|
|
|
var isAuthenticated = httpContext.User.Identity?.IsAuthenticated ?? false; |
|
|
|
var authenticationSchemeProvider = httpContext.RequestServices.GetRequiredService<IAuthenticationSchemeProvider>(); |
|
|
|
|
|
|
|
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<IAuthenticationHandlerProvider>(); |
|
|
|
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<IAuthenticationHandlerProvider>(); |
|
|
|
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); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|