Skip to content

Commit

Permalink
Merge pull request abpframework#16140 from abpframework/RedirectCookies
Browse files Browse the repository at this point in the history
Redirect to current url after `SignOut` cookies.
  • Loading branch information
maliming authored Mar 31, 2023
2 parents 053c3c1 + f5df581 commit 6405df8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
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;
Expand All @@ -28,6 +30,7 @@ public AbpAspNetCoreMultiTenancyOptions()
TenantKey = TenantResolverConsts.DefaultTenantKey;
MultiTenancyMiddlewareErrorPageBuilder = async (context, exception) =>
{
var isCookieAuthentication = false;
var tenantResolveResult = context.RequestServices.GetRequiredService<ITenantResolveResultAccessor>().Result;
if (tenantResolveResult != null)
{
Expand All @@ -37,10 +40,11 @@ public 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;
}
}
}
Expand All @@ -54,19 +58,27 @@ public 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.Request.Method.Equals("Get", StringComparison.OrdinalIgnoreCase) && !context.Request.IsAjax())
{
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;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
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;
Expand All @@ -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;
Expand All @@ -26,6 +30,8 @@ public MultiTenancyMiddleware(
IOptions<AbpAspNetCoreMultiTenancyOptions> options,
ITenantResolveResultAccessor tenantResolveResultAccessor)
{
Logger = NullLogger<MultiTenancyMiddleware>.Instance;

_tenantConfigurationProvider = tenantConfigurationProvider;
_currentTenant = currentTenant;
_tenantResolveResultAccessor = tenantResolveResultAccessor;
Expand All @@ -41,6 +47,8 @@ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
}
catch (Exception e)
{
Logger.LogException(e);

if (await _options.MultiTenancyMiddlewareErrorPageBuilder(context, e))
{
return;
Expand Down

0 comments on commit 6405df8

Please sign in to comment.