diff --git a/Oqtane.Client/App.razor b/Oqtane.Client/App.razor index 82a0b24a..5ad3a790 100644 --- a/Oqtane.Client/App.razor +++ b/Oqtane.Client/App.razor @@ -47,11 +47,13 @@ protected override async Task OnParametersSetAsync() { + SiteState.AntiForgeryToken = AntiForgeryToken; + InstallationService.SetAntiForgeryTokenHeader(AntiForgeryToken); + _installation = await InstallationService.IsInstalled(); if (_installation.Alias != null) { SiteState.Alias = _installation.Alias; - SiteState.AntiForgeryToken = AntiForgeryToken; } else { @@ -68,8 +70,10 @@ { // parameter values are not set when running on WebAssembly (seems to be a .NET 5 bug) - need to retrieve using JSInterop var interop = new Interop(JSRuntime); - AntiForgeryToken = await interop.GetElementByName(Constants.RequestVerificationToken); - SiteState.AntiForgeryToken = AntiForgeryToken; + + SiteState.AntiForgeryToken = await interop.GetElementByName(Constants.RequestVerificationToken); + InstallationService.SetAntiForgeryTokenHeader(SiteState.AntiForgeryToken); + Runtime = await interop.GetElementByName("app_runtime"); RenderMode = await interop.GetElementByName("app_rendermode"); } diff --git a/Oqtane.Client/Services/InstallationService.cs b/Oqtane.Client/Services/InstallationService.cs index b2943bc0..a07aa754 100644 --- a/Oqtane.Client/Services/InstallationService.cs +++ b/Oqtane.Client/Services/InstallationService.cs @@ -25,8 +25,6 @@ namespace Oqtane.Services public async Task IsInstalled() { - // add antiforgerytoken header so that it is included on all HttpClient calls for the lifetime of the app - AddRequestHeader(Constants.AntiForgeryTokenHeaderName, _siteState.AntiForgeryToken); var path = new Uri(_navigationManager.Uri).LocalPath.Substring(1); return await GetJsonAsync($"{ApiUrl}/installed/?path={WebUtility.UrlEncode(path)}"); } @@ -50,5 +48,14 @@ namespace Oqtane.Services { await PostJsonAsync($"{ApiUrl}/register?email={WebUtility.UrlEncode(email)}", true); } + + public void SetAntiForgeryTokenHeader(string antiforgerytokenvalue) + { + if (!string.IsNullOrEmpty(antiforgerytokenvalue)) + { + AddRequestHeader(Constants.AntiForgeryTokenHeaderName, antiforgerytokenvalue); + } + } + } } diff --git a/Oqtane.Client/Services/Interfaces/IInstallationService.cs b/Oqtane.Client/Services/Interfaces/IInstallationService.cs index e3239b76..252b1609 100644 --- a/Oqtane.Client/Services/Interfaces/IInstallationService.cs +++ b/Oqtane.Client/Services/Interfaces/IInstallationService.cs @@ -41,5 +41,11 @@ namespace Oqtane.Services /// Email of the user to be registered /// Task RegisterAsync(string email); + + /// + /// Sets the antiforgerytoken header so that it is included on all HttpClient calls for the lifetime of the app + /// + /// + void SetAntiForgeryTokenHeader(string antiforgerytokenvalue); } }