diff --git a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs index f4820073fd4..5956e4f7d36 100644 --- a/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs +++ b/framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs @@ -4,7 +4,9 @@ using System.Text.Encodings.Web; using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; +using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Extensions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using Volo.Abp.MultiTenancy; @@ -28,6 +30,7 @@ public AbpAspNetCoreMultiTenancyOptions() TenantKey = TenantResolverConsts.DefaultTenantKey; MultiTenancyMiddlewareErrorPageBuilder = async (context, exception) => { + var isCookieAuthentication = false; var tenantResolveResult = context.RequestServices.GetRequiredService().Result; if (tenantResolveResult != null) { @@ -37,10 +40,11 @@ public AbpAspNetCoreMultiTenancyOptions() if (authenticationType != null) { var scheme = await context.RequestServices.GetRequiredService().GetHandlerAsync(context, authenticationType); - if (scheme is IAuthenticationSignOutHandler signOutHandler) + if (scheme is CookieAuthenticationHandler cookieAuthenticationHandler) { // Try to delete the authentication's cookie if it does not exist or is inactive. - await signOutHandler.SignOutAsync(null); + await cookieAuthenticationHandler.SignOutAsync(null); + isCookieAuthentication = true; } } } @@ -54,19 +58,27 @@ public AbpAspNetCoreMultiTenancyOptions() } } - context.Response.Headers.Add("Abp-Tenant-Resolve-Error", exception.Message); - context.Response.StatusCode = (int)HttpStatusCode.NotFound; - context.Response.ContentType = "text/html"; + if (isCookieAuthentication && context.Request.Method.Equals("Get", StringComparison.OrdinalIgnoreCase) && !context.Request.IsAjax()) + { + context.Response.Headers.Add("Abp-Tenant-Resolve-Error", exception.Message); + context.Response.Redirect(context.Request.GetEncodedUrl()); + } + else + { + context.Response.Headers.Add("Abp-Tenant-Resolve-Error", exception.Message); + context.Response.StatusCode = (int)HttpStatusCode.NotFound; + context.Response.ContentType = "text/html"; - var message = exception.Message; - var details = exception is BusinessException businessException ? businessException.Details : string.Empty; + var message = exception.Message; + var details = exception is BusinessException businessException ? businessException.Details : string.Empty; - await context.Response.WriteAsync($"\r\n"); - await context.Response.WriteAsync($"

{HtmlEncoder.Default.Encode(message)}

{HtmlEncoder.Default.Encode(details)}
\r\n"); - await context.Response.WriteAsync("\r\n"); + await context.Response.WriteAsync($"\r\n"); + await context.Response.WriteAsync($"

{HtmlEncoder.Default.Encode(message)}

{HtmlEncoder.Default.Encode(details)}
\r\n"); + await context.Response.WriteAsync("\r\n"); - // Note the 500 spaces are to work around an IE 'feature' - await context.Response.WriteAsync(new string(' ', 500)); + // Note the 500 spaces are to work around an IE 'feature' + await context.Response.WriteAsync(new string(' ', 500)); + } return true; }; 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 00b3f87056c..89ae55b3b1f 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 @@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.RequestLocalization; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Localization; @@ -15,6 +17,8 @@ namespace Volo.Abp.AspNetCore.MultiTenancy; public class MultiTenancyMiddleware : IMiddleware, ITransientDependency { + public ILogger Logger { get; set; } + private readonly ITenantConfigurationProvider _tenantConfigurationProvider; private readonly ICurrentTenant _currentTenant; private readonly AbpAspNetCoreMultiTenancyOptions _options; @@ -26,6 +30,8 @@ public MultiTenancyMiddleware( IOptions options, ITenantResolveResultAccessor tenantResolveResultAccessor) { + Logger = NullLogger.Instance; + _tenantConfigurationProvider = tenantConfigurationProvider; _currentTenant = currentTenant; _tenantResolveResultAccessor = tenantResolveResultAccessor; @@ -41,6 +47,8 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next) } catch (Exception e) { + Logger.LogException(e); + if (await _options.MultiTenancyMiddlewareErrorPageBuilder(context, e)) { return;