Browse Source

Merge pull request #16142 from abpframework/auto-merge/rel-7-0/1841

pull/16143/head
maliming 4 years ago
committed by GitHub
parent
commit
b8e94f9852
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 36
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs
  2. 8
      framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs

36
framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/AbpAspNetCoreMultiTenancyOptions.cs

@ -4,7 +4,9 @@ using System.Net;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.MultiTenancy; using Volo.Abp.MultiTenancy;
@ -28,6 +30,7 @@ public class AbpAspNetCoreMultiTenancyOptions
TenantKey = TenantResolverConsts.DefaultTenantKey; TenantKey = TenantResolverConsts.DefaultTenantKey;
MultiTenancyMiddlewareErrorPageBuilder = async (context, exception) => MultiTenancyMiddlewareErrorPageBuilder = async (context, exception) =>
{ {
var isCookieAuthentication = false;
var tenantResolveResult = context.RequestServices.GetRequiredService<ITenantResolveResultAccessor>().Result; var tenantResolveResult = context.RequestServices.GetRequiredService<ITenantResolveResultAccessor>().Result;
if (tenantResolveResult != null) if (tenantResolveResult != null)
{ {
@ -37,10 +40,11 @@ public class AbpAspNetCoreMultiTenancyOptions
if (authenticationType != null) if (authenticationType != null)
{ {
var scheme = await context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>().GetHandlerAsync(context, authenticationType); var scheme = await context.RequestServices.GetRequiredService<IAuthenticationHandlerProvider>().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. // 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 class AbpAspNetCoreMultiTenancyOptions
} }
} }
context.Response.Headers.Add("Abp-Tenant-Resolve-Error", exception.Message); if (isCookieAuthentication && context.Request.Method.Equals("Get", StringComparison.OrdinalIgnoreCase) && !context.Request.IsAjax())
context.Response.StatusCode = (int)HttpStatusCode.NotFound; {
context.Response.ContentType = "text/html"; 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 message = exception.Message;
var details = exception is BusinessException businessException ? businessException.Details : string.Empty; var details = exception is BusinessException businessException ? businessException.Details : string.Empty;
await context.Response.WriteAsync($"<html lang=\"{HtmlEncoder.Default.Encode(CultureInfo.CurrentCulture.Name)}\"><body>\r\n"); await context.Response.WriteAsync($"<html lang=\"{HtmlEncoder.Default.Encode(CultureInfo.CurrentCulture.Name)}\"><body>\r\n");
await context.Response.WriteAsync($"<h3>{HtmlEncoder.Default.Encode(message)}</h3>{HtmlEncoder.Default.Encode(details)}<br>\r\n"); await context.Response.WriteAsync($"<h3>{HtmlEncoder.Default.Encode(message)}</h3>{HtmlEncoder.Default.Encode(details)}<br>\r\n");
await context.Response.WriteAsync("</body></html>\r\n"); await context.Response.WriteAsync("</body></html>\r\n");
// Note the 500 spaces are to work around an IE 'feature' // Note the 500 spaces are to work around an IE 'feature'
await context.Response.WriteAsync(new string(' ', 500)); await context.Response.WriteAsync(new string(' ', 500));
}
return true; return true;
}; };

8
framework/src/Volo.Abp.AspNetCore.MultiTenancy/Volo/Abp/AspNetCore/MultiTenancy/MultiTenancyMiddleware.cs

@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization; using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.RequestLocalization; using Microsoft.AspNetCore.RequestLocalization;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.Localization; using Volo.Abp.Localization;
@ -15,6 +17,8 @@ namespace Volo.Abp.AspNetCore.MultiTenancy;
public class MultiTenancyMiddleware : IMiddleware, ITransientDependency public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
{ {
public ILogger<MultiTenancyMiddleware> Logger { get; set; }
private readonly ITenantConfigurationProvider _tenantConfigurationProvider; private readonly ITenantConfigurationProvider _tenantConfigurationProvider;
private readonly ICurrentTenant _currentTenant; private readonly ICurrentTenant _currentTenant;
private readonly AbpAspNetCoreMultiTenancyOptions _options; private readonly AbpAspNetCoreMultiTenancyOptions _options;
@ -26,6 +30,8 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
IOptions<AbpAspNetCoreMultiTenancyOptions> options, IOptions<AbpAspNetCoreMultiTenancyOptions> options,
ITenantResolveResultAccessor tenantResolveResultAccessor) ITenantResolveResultAccessor tenantResolveResultAccessor)
{ {
Logger = NullLogger<MultiTenancyMiddleware>.Instance;
_tenantConfigurationProvider = tenantConfigurationProvider; _tenantConfigurationProvider = tenantConfigurationProvider;
_currentTenant = currentTenant; _currentTenant = currentTenant;
_tenantResolveResultAccessor = tenantResolveResultAccessor; _tenantResolveResultAccessor = tenantResolveResultAccessor;
@ -41,6 +47,8 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
} }
catch (Exception e) catch (Exception e)
{ {
Logger.LogException(e);
if (await _options.MultiTenancyMiddlewareErrorPageBuilder(context, e)) if (await _options.MultiTenancyMiddlewareErrorPageBuilder(context, e))
{ {
return; return;

Loading…
Cancel
Save