Browse Source

Redirect to home page when get a `403` response.

pull/19605/head
maliming 2 years ago
parent
commit
e87af151ff
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 46
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs

46
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs

@ -24,34 +24,46 @@ public class ClientProxyExceptionEventHandler : ILocalEventHandler<ClientProxyEx
public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData) public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData)
{ {
if (eventData.StatusCode == 401) using (var scope = ServiceProvider.CreateScope())
{ {
using (var scope = ServiceProvider.CreateScope()) switch (eventData.StatusCode)
{ {
var options = scope.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreComponentsWebOptions>>(); case 401:
if (!options.Value.IsBlazorWebApp)
{ {
var navigationManager = scope.ServiceProvider.GetRequiredService<NavigationManager>(); var options = scope.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreComponentsWebOptions>>();
var accessTokenProvider = scope.ServiceProvider.GetRequiredService<IAccessTokenProvider>();
var authenticationOptions = scope.ServiceProvider.GetRequiredService<IOptions<AbpAuthenticationOptions>>(); if (!options.Value.IsBlazorWebApp)
var result = await accessTokenProvider.RequestAccessToken();
if (result.Status != AccessTokenResultStatus.Success)
{ {
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); var navigationManager = scope.ServiceProvider.GetRequiredService<NavigationManager>();
return; var accessTokenProvider = scope.ServiceProvider.GetRequiredService<IAccessTokenProvider>();
} var authenticationOptions = scope.ServiceProvider.GetRequiredService<IOptions<AbpAuthenticationOptions>>();
var result = await accessTokenProvider.RequestAccessToken();
if (result.Status != AccessTokenResultStatus.Success)
{
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl);
return;
}
result.TryGetToken(out var token); result.TryGetToken(out var token);
if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5)) if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5))
{
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl);
}
}
else
{ {
navigationManager.NavigateToLogout(authenticationOptions.Value.LogoutUrl); var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>();
await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)");
} }
break;
} }
else case 403:
{ {
var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>(); var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>();
await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)"); await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)");
break;
} }
} }
} }

Loading…
Cancel
Save