mirror of https://github.com/abpframework/abp.git
9 changed files with 159 additions and 2 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,65 @@ |
|||
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 AbpAuthenticationSessionState : ComponentBase |
|||
{ |
|||
private const string SessionKey = "authentication-session-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() |
|||
{ |
|||
var sessionId = await LocalStorage.GetItemAsync(SessionKey); |
|||
if (sessionId.IsNullOrWhiteSpace()) |
|||
{ |
|||
sessionId = CurrentUser.FindSessionId() ?? Guid.NewGuid().ToString(); |
|||
} |
|||
|
|||
await LocalStorage.SetItemAsync(SessionKey, sessionId); |
|||
} |
|||
|
|||
protected virtual async Task ClearAuthenticationStateAsync() |
|||
{ |
|||
await LocalStorage.RemoveItemAsync(SessionKey); |
|||
} |
|||
} |
|||
@ -0,0 +1,27 @@ |
|||
(function () { |
|||
|
|||
var sessionKey = 'authentication-session-id'; |
|||
|
|||
window.addEventListener('storage', function (event) { |
|||
|
|||
console.log(event); |
|||
if (event.key !== sessionKey) { |
|||
return; |
|||
} |
|||
|
|||
var previousSessionId = event.oldValue |
|||
var sessionId = event.newValue; |
|||
|
|||
if(previousSessionId === sessionId) { |
|||
return; |
|||
} |
|||
|
|||
if(previousSessionId || !sessionId) { |
|||
abp.utils.removeOidcUser(); |
|||
window.location.reload(); |
|||
return; |
|||
} |
|||
|
|||
location.assign('/') |
|||
}); |
|||
}()); |
|||
Loading…
Reference in new issue