Browse Source

Add MultiTenancyMiddlewareErrorPageBuilder to show exception.

Resolve #3503
pull/8916/head
maliming 5 years ago
parent
commit
93603dbaa3
  1. 26
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs
  2. 21
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs
  3. 14
      modules/identity/src/Volo.Abp.Identity.AspNetCore/Volo/Abp/Identity/AspNetCore/AbpSecurityStampValidator.cs

26
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
/// </summary>
public string TenantKey { get; set; }
public Func<HttpContext, Exception, Task> 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($"<html lang=\"{CultureInfo.CurrentCulture.Name}\"><body>\r\n");
await context.Response.WriteAsync($"<h3>{message}</h3>{details}<br>\r\n");
await context.Response.WriteAsync("</body></html>\r\n");
// Note the 500 spaces are to work around an IE 'feature'
await context.Response.WriteAsync(new string(' ', 500));
};
}
}
}
}

21
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<AbpAspNetCoreMultiTenancyOptions> 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))

14
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);

Loading…
Cancel
Save