Browse Source

[Blazor wasm]`logout` if token expired.

pull/16602/head
maliming 3 years ago
parent
commit
1a3ba1146a
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 4
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs
  2. 40
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/ClientProxyExceptionEventHandler.cs
  3. 8
      framework/src/Volo.Abp.Http.Abstractions/Volo/Abp/Http/ClientProxyExceptionEventData.cs
  4. 1
      framework/src/Volo.Abp.Http.Client/Volo.Abp.Http.Client.csproj
  5. 4
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientModule.cs
  6. 12
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs

4
framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs

@ -8,6 +8,7 @@ using Volo.Abp.AspNetCore.Components.Web.ExceptionHandling;
using Volo.Abp.AspNetCore.Components.Web.Security;
using Volo.Abp.AspNetCore.Mvc.Client;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Volo.Abp.Http.Client;
using Volo.Abp.Modularity;
using Volo.Abp.Threading;
@ -18,7 +19,8 @@ namespace Volo.Abp.AspNetCore.Components.WebAssembly;
[DependsOn(
typeof(AbpAspNetCoreMvcClientCommonModule),
typeof(AbpUiModule),
typeof(AbpAspNetCoreComponentsWebModule)
typeof(AbpAspNetCoreComponentsWebModule),
typeof(AbpEventBusModule)
)]
public class AbpAspNetCoreComponentsWebAssemblyModule : AbpModule
{

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

@ -0,0 +1,40 @@
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Volo.Abp.Http;
namespace Volo.Abp.AspNetCore.Components.WebAssembly;
public class ClientProxyExceptionEventHandler : ILocalEventHandler<ClientProxyExceptionEventData>, ITransientDependency
{
protected NavigationManager NavigationManager { get; }
protected IAccessTokenProvider AccessTokenProvider { get; }
public ClientProxyExceptionEventHandler(NavigationManager navigationManager, IAccessTokenProvider accessTokenProvider)
{
NavigationManager = navigationManager;
AccessTokenProvider = accessTokenProvider;
}
public virtual async Task HandleEventAsync(ClientProxyExceptionEventData eventData)
{
if (eventData.StatusCode == 401)
{
var result = await AccessTokenProvider.RequestAccessToken();
if (result.Status != AccessTokenResultStatus.Success)
{
NavigationManager.NavigateToLogout("authentication/logout");
return;
}
result.TryGetToken(out var token);
if (token != null && DateTimeOffset.Now >= token.Expires.AddMinutes(-5))
{
NavigationManager.NavigateToLogout("authentication/logout");
}
}
}
}

8
framework/src/Volo.Abp.Http.Abstractions/Volo/Abp/Http/ClientProxyExceptionEventData.cs

@ -0,0 +1,8 @@
namespace Volo.Abp.Http;
public class ClientProxyExceptionEventData
{
public int? StatusCode { get; set; }
public string ReasonPhrase { get; set; }
}

1
framework/src/Volo.Abp.Http.Client/Volo.Abp.Http.Client.csproj

@ -20,6 +20,7 @@
<ItemGroup>
<ProjectReference Include="..\Volo.Abp.Castle.Core\Volo.Abp.Castle.Core.csproj" />
<ProjectReference Include="..\Volo.Abp.EventBus\Volo.Abp.EventBus.csproj" />
<ProjectReference Include="..\Volo.Abp.ExceptionHandling\Volo.Abp.ExceptionHandling.csproj" />
<ProjectReference Include="..\Volo.Abp.Http\Volo.Abp.Http.csproj" />
<ProjectReference Include="..\Volo.Abp.MultiTenancy\Volo.Abp.MultiTenancy.csproj" />

4
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientModule.cs

@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.Castle;
using Volo.Abp.EventBus;
using Volo.Abp.Modularity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Threading;
@ -17,7 +18,8 @@ namespace Volo.Abp.Http.Client;
typeof(AbpMultiTenancyModule),
typeof(AbpValidationModule),
typeof(AbpExceptionHandlingModule),
typeof(AbpRemoteServicesModule)
typeof(AbpRemoteServicesModule),
typeof(AbpEventBusModule)
)]
public class AbpHttpClientModule : AbpModule
{

12
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
@ -10,6 +10,7 @@ using Microsoft.Extensions.Options;
using Microsoft.Extensions.Primitives;
using Volo.Abp.Content;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Http.Client.Authentication;
using Volo.Abp.Http.Client.Proxying;
using Volo.Abp.Http.Modeling;
@ -39,6 +40,7 @@ public class ClientProxyBase<TService> : ITransientDependency
protected ClientProxyRequestPayloadBuilder ClientProxyRequestPayloadBuilder => LazyServiceProvider.LazyGetRequiredService<ClientProxyRequestPayloadBuilder>();
protected ClientProxyUrlBuilder ClientProxyUrlBuilder => LazyServiceProvider.LazyGetRequiredService<ClientProxyUrlBuilder>();
protected ICurrentApiVersionInfo CurrentApiVersionInfo => LazyServiceProvider.LazyGetRequiredService<ICurrentApiVersionInfo>();
protected ILocalEventBus LocalEventBus => LazyServiceProvider.LazyGetRequiredService<ILocalEventBus>();
protected virtual async Task RequestAsync(string methodName, ClientProxyRequestTypeValue arguments = null)
{
@ -66,7 +68,7 @@ public class ClientProxyBase<TService> : ITransientDependency
var actionArguments = action.Parameters.GroupBy(x => x.NameOnMethod).ToList();
if (action.SupportedVersions.Any())
{
{
//TODO: make names configurable
actionArguments.RemoveAll(x => x.Key == "api-version" || x.Key == "apiVersion");
}
@ -216,6 +218,12 @@ public class ClientProxyBase<TService> : ITransientDependency
protected virtual async Task ThrowExceptionForResponseAsync(HttpResponseMessage response)
{
await LocalEventBus.PublishAsync(new ClientProxyExceptionEventData()
{
StatusCode = (int?)response?.StatusCode,
ReasonPhrase = response?.ReasonPhrase
});
if (response.Headers.Contains(AbpHttpConsts.AbpErrorFormat))
{
RemoteServiceErrorResponse errorResponse;

Loading…
Cancel
Save