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 63b83885f9..b2ecf708c5 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 @@ -1,4 +1,9 @@ -using Volo.Abp.MultiTenancy; +using System; +using System.Globalization; +using System.Net; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Http; +using Volo.Abp.MultiTenancy; namespace Volo.Abp.AspNetCore.MultiTenancy { @@ -9,9 +14,26 @@ namespace Volo.Abp.AspNetCore.MultiTenancy /// public string TenantKey { get; set; } + public Func MultiTenancyMiddlewareErrorPageBuilder { get; set; } + public AbpAspNetCoreMultiTenancyOptions() { TenantKey = TenantResolverConsts.DefaultTenantKey; + MultiTenancyMiddlewareErrorPageBuilder = async (context, exception)=> + { + context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;; + context.Response.ContentType = "text/html"; + + var message = exception.Message; + var details = exception is BusinessException businessException ? businessException.Details : string.Empty; + + await context.Response.WriteAsync($"\r\n"); + await context.Response.WriteAsync($"

{message}

{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)); + }; } } -} \ No newline at end of file +} 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 eaf5c753fd..361826672b 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 @@ -1,10 +1,14 @@ using System; using System.Globalization; +using System.IO; +using System.Net; using System.Threading.Tasks; +using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.RequestLocalization; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Options; using Volo.Abp.DependencyInjection; using Volo.Abp.Localization; using Volo.Abp.MultiTenancy; @@ -16,18 +20,31 @@ namespace Volo.Abp.AspNetCore.MultiTenancy { private readonly ITenantConfigurationProvider _tenantConfigurationProvider; private readonly ICurrentTenant _currentTenant; + private readonly AbpAspNetCoreMultiTenancyOptions _options; public MultiTenancyMiddleware( ITenantConfigurationProvider tenantConfigurationProvider, - ICurrentTenant currentTenant) + ICurrentTenant currentTenant, + IOptions options) { _tenantConfigurationProvider = tenantConfigurationProvider; _currentTenant = currentTenant; + _options = options.Value; } public async Task InvokeAsync(HttpContext context, RequestDelegate next) { - var tenant = await _tenantConfigurationProvider.GetAsync(saveResolveResult: true); + TenantConfiguration tenant; + try + { + tenant = await _tenantConfigurationProvider.GetAsync(saveResolveResult: true); + } + catch (Exception e) + { + await _options.MultiTenancyMiddlewareErrorPageBuilder(context, e); + return; + } + if (tenant?.Id != _currentTenant.Id) { using (_currentTenant.Change(tenant?.Id, tenant?.Name)) diff --git a/modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSecurityStampValidator.cs b/modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSecurityStampValidator.cs index 1dcc624a47..8ae543bed2 100644 --- a/modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSecurityStampValidator.cs +++ b/modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSecurityStampValidator.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Identity; @@ -34,7 +35,16 @@ namespace Volo.Abp.Identity.AspNetCore [UnitOfWork] public override async Task ValidateAsync(CookieValidatePrincipalContext context) { - var tenant = await TenantConfigurationProvider.GetAsync(saveResolveResult: false); + TenantConfiguration tenant = null; + try + { + tenant = await TenantConfigurationProvider.GetAsync(saveResolveResult: false); + } + catch (Exception e) + { + Logger.LogException(e); + } + using (CurrentTenant.Change(tenant?.Id, tenant?.Name)) { await base.ValidateAsync(context);