Browse Source

Update ClientProxyExceptionEventHandler.cs

pull/18876/head
maliming 3 years ago
parent
commit
915e65be78
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 44
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs

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

@ -2,6 +2,10 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication; using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Microsoft.JSInterop;
using Volo.Abp.AspNetCore.Components.Server;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus; using Volo.Abp.EventBus;
using Volo.Abp.Http; using Volo.Abp.Http;
@ -10,30 +14,42 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public class ClientProxyExceptionEventHandler : ILocalEventHandler<ClientProxyExceptionEventData>, ITransientDependency public class ClientProxyExceptionEventHandler : ILocalEventHandler<ClientProxyExceptionEventData>, ITransientDependency
{ {
protected NavigationManager NavigationManager { get; } protected IServiceProvider ServiceProvider { get; }
protected IAccessTokenProvider AccessTokenProvider { get; }
public ClientProxyExceptionEventHandler(NavigationManager navigationManager, IAccessTokenProvider accessTokenProvider) public ClientProxyExceptionEventHandler(IServiceProvider serviceProvider)
{ {
NavigationManager = navigationManager; ServiceProvider = serviceProvider;
AccessTokenProvider = accessTokenProvider;
} }
public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData) public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData)
{ {
if (eventData.StatusCode == 401) if (eventData.StatusCode == 401)
{ {
var result = await AccessTokenProvider.RequestAccessToken(); using (var scope = ServiceProvider.CreateScope())
if (result.Status != AccessTokenResultStatus.Success)
{ {
NavigationManager.NavigateToLogout("authentication/logout"); var options = scope.ServiceProvider.GetRequiredService<IOptions<AbpAspNetCoreComponentsWebOptions>>();
return; if (!options.Value.IsBlazorWebApp)
} {
var navigationManager = scope.ServiceProvider.GetRequiredService<NavigationManager>();
var accessTokenProvider = scope.ServiceProvider.GetRequiredService<IAccessTokenProvider>();
var result = await accessTokenProvider.RequestAccessToken();
if (result.Status != AccessTokenResultStatus.Success)
{
navigationManager.NavigateToLogout("authentication/logout");
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("authentication/logout"); navigationManager.NavigateToLogout("authentication/logout");
}
}
else
{
var jsRuntime = scope.ServiceProvider.GetRequiredService<IJSRuntime>();
await jsRuntime.InvokeVoidAsync("eval", "setTimeout(function(){location.assign('/')}, 2000)");
}
} }
} }
} }

Loading…
Cancel
Save