Redirect to current url after `SignOut` cookies.

pull/16140/head
maliming 3 years ago
parent 053c3c1e01
commit 7ca5d57ddf
No known key found for this signature in database
GPG Key ID: A646B9CB645ECEA4

@ -4,7 +4,9 @@ using System.Net;
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 class AbpAspNetCoreMultiTenancyOptions
TenantKey = TenantResolverConsts.DefaultTenantKey;
MultiTenancyMiddlewareErrorPageBuilder = async (context, exception) =>
{
var isCookieAuthentication = false;
var tenantResolveResult = context.RequestServices.GetRequiredService<ITenantResolveResultAccessor>().Result;
if (tenantResolveResult != null)
{
@ -37,10 +40,11 @@ public class AbpAspNetCoreMultiTenancyOptions
if (authenticationType != null)
{
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.
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);
context.Response.StatusCode = (int)HttpStatusCode.NotFound;
context.Response.ContentType = "text/html";
if (isCookieAuthentication)
{
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($"<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("</body></html>\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("</body></html>\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;
};

@ -5,6 +5,8 @@ using Microsoft.AspNetCore.Http;
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<MultiTenancyMiddleware> Logger { get; set; }
private readonly ITenantConfigurationProvider _tenantConfigurationProvider;
private readonly ICurrentTenant _currentTenant;
private readonly AbpAspNetCoreMultiTenancyOptions _options;
@ -26,6 +30,8 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
IOptions<AbpAspNetCoreMultiTenancyOptions> options,
ITenantResolveResultAccessor tenantResolveResultAccessor)
{
Logger = NullLogger<MultiTenancyMiddleware>.Instance;
_tenantConfigurationProvider = tenantConfigurationProvider;
_currentTenant = currentTenant;
_tenantResolveResultAccessor = tenantResolveResultAccessor;
@ -41,6 +47,8 @@ public class MultiTenancyMiddleware : IMiddleware, ITransientDependency
}
catch (Exception e)
{
Logger.LogException(e);
if (await _options.MultiTenancyMiddlewareErrorPageBuilder(context, e))
{
return;

Loading…
Cancel
Save