mirror of https://github.com/abpframework/abp.git
committed by
GitHub
19 changed files with 204 additions and 44 deletions
@ -0,0 +1,8 @@ |
|||
namespace Volo.Abp.AspNetCore.Components.Web; |
|||
|
|||
public class AbpAuthenticationOptions |
|||
{ |
|||
public string LoginUrl { get; set; } = "Account/Login"; |
|||
|
|||
public string LogoutUrl { get; set; } = "Account/Logout"; |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web; |
|||
|
|||
public interface ILocalStorageService |
|||
{ |
|||
public ValueTask SetItemAsync(string key, string value); |
|||
public ValueTask<string> GetItemAsync(string key); |
|||
public ValueTask RemoveItemAsync(string key); |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System.Threading.Tasks; |
|||
using Microsoft.JSInterop; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web; |
|||
|
|||
[Dependency(ReplaceServices = true)] |
|||
public class LocalStorageService : ILocalStorageService, ITransientDependency |
|||
{ |
|||
public IJSRuntime JsRuntime { get; } |
|||
|
|||
public LocalStorageService(IJSRuntime jsRuntime) |
|||
{ |
|||
JsRuntime = jsRuntime; |
|||
} |
|||
|
|||
public async ValueTask SetItemAsync(string key, string value) |
|||
{ |
|||
await JsRuntime.InvokeVoidAsync("localStorage.setItem", key, value); |
|||
} |
|||
|
|||
public async ValueTask<string> GetItemAsync(string key) |
|||
{ |
|||
return await JsRuntime.InvokeAsync<string>("localStorage.getItem", key); |
|||
} |
|||
|
|||
public async ValueTask RemoveItemAsync(string key) |
|||
{ |
|||
await JsRuntime.InvokeVoidAsync("localStorage.removeItem", key); |
|||
} |
|||
} |
|||
@ -0,0 +1,59 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Components; |
|||
using Microsoft.AspNetCore.Components.Routing; |
|||
using Microsoft.Extensions.Options; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.Web.Security; |
|||
|
|||
public class AbpAuthenticationState : ComponentBase |
|||
{ |
|||
private const string StateKey = "authentication-state-id"; |
|||
|
|||
[Inject] |
|||
protected ILocalStorageService LocalStorage { get; set; } = default!; |
|||
|
|||
[Inject] |
|||
protected ICurrentUser CurrentUser { get; set; } = default!; |
|||
|
|||
[Inject] |
|||
protected NavigationManager NavigationManager { get; set; } = default!; |
|||
|
|||
[Inject] |
|||
protected IOptions<AbpAuthenticationOptions> AuthenticationOptions { get; set; } = default!; |
|||
|
|||
protected async override Task OnAfterRenderAsync(bool firstRender) |
|||
{ |
|||
if (firstRender) |
|||
{ |
|||
NavigationManager.RegisterLocationChangingHandler(OnLocationChangingAsync); |
|||
if (CurrentUser.IsAuthenticated) |
|||
{ |
|||
await SetAuthenticationStateAsync(); |
|||
} |
|||
else |
|||
{ |
|||
await ClearAuthenticationStateAsync(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
protected virtual async ValueTask OnLocationChangingAsync(LocationChangingContext context) |
|||
{ |
|||
if (context.TargetLocation.Contains(AuthenticationOptions.Value.LogoutUrl)) |
|||
{ |
|||
await ClearAuthenticationStateAsync(); |
|||
} |
|||
} |
|||
|
|||
protected virtual async Task SetAuthenticationStateAsync() |
|||
{ |
|||
await LocalStorage.SetItemAsync(StateKey, CurrentUser.GetId().ToString()); |
|||
} |
|||
|
|||
protected virtual async Task ClearAuthenticationStateAsync() |
|||
{ |
|||
await LocalStorage.RemoveItemAsync(StateKey); |
|||
} |
|||
} |
|||
@ -0,0 +1,17 @@ |
|||
(function () { |
|||
|
|||
const stateKey = 'authentication-state-id'; |
|||
|
|||
window.addEventListener('storage', function (event) { |
|||
if (event.key !== stateKey || event.oldValue === event.newValue) { |
|||
return; |
|||
} |
|||
|
|||
if (event.oldValue || !event.newValue) { |
|||
abp.utils.removeOidcUser(); |
|||
window.location.reload(); |
|||
} else { |
|||
location.assign('/') |
|||
} |
|||
}); |
|||
}()); |
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue