Merge pull request #8916 from abpframework/maliming/MultiTenancyMiddlewareErrorPageBuilder

Add MultiTenancyMiddlewareErrorPageBuilder to show exception.
pull/9048/head
Halil İbrahim Kalkan 4 years ago committed by GitHub
commit 867e527b5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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));
};
}
}
}
}

@ -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))

@ -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