From 89bdfd762f2049d74afd9a7ac5b7c7d6d274277b Mon Sep 17 00:00:00 2001 From: AlexanderKovalenko Date: Tue, 10 Nov 2026 14:23:42 +0400 Subject: [PATCH 1/4] Update example for SignalR transport --- CS/BlazorAuth/BlazorAuth.csproj | 91 ++++++++++++++++++- CS/BlazorAuth/Code/AuthHelper.cs | 18 ++++ .../Code/CustomConnectionStringProvider.cs | 8 +- CS/BlazorAuth/Code/CustomDBSchemaProvider.cs | 10 +- CS/BlazorAuth/Code/CustomDashboardStorage.cs | 16 ++-- CS/BlazorAuth/Code/CustomDataSourceStorage.cs | 11 ++- .../Code/MultiTenantDashboardConfigurator.cs | 29 +++--- CS/BlazorAuth/Pages/Dashboard.razor | 11 +-- CS/BlazorAuth/Program.cs | 17 ++-- README.md | 8 +- 10 files changed, 164 insertions(+), 55 deletions(-) create mode 100644 CS/BlazorAuth/Code/AuthHelper.cs diff --git a/CS/BlazorAuth/BlazorAuth.csproj b/CS/BlazorAuth/BlazorAuth.csproj index 75744e0..30c2e77 100644 --- a/CS/BlazorAuth/BlazorAuth.csproj +++ b/CS/BlazorAuth/BlazorAuth.csproj @@ -4,11 +4,95 @@ net10.0 enable enable - aspnet-BlazorAuth-FEB431D8-D991-4F8A-923F-40225A99A530 + + + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Blazor.Resources.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.Blazor.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.AspNetCore.Common.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.AspNetCore.Core.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Web.Resources.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Charts.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.AspNetCore.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.Web.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Data.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.DataAccess.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.CodeParser.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Map.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Office.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Drawing.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.PivotGrid.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Pdf.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Printing.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.RichEdit.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Xpo.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.XtraReports.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.XtraCharts.v26.2.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.XtraGauges.v26.2.Core.dll + + + C:\Work\dxvcs\Bin\NETCore\DevExpress.Sparkline.v26.2.Core.dll + + + + + + + + + + + - - + @@ -17,5 +101,6 @@ + \ No newline at end of file diff --git a/CS/BlazorAuth/Code/AuthHelper.cs b/CS/BlazorAuth/Code/AuthHelper.cs new file mode 100644 index 0000000..4dde826 --- /dev/null +++ b/CS/BlazorAuth/Code/AuthHelper.cs @@ -0,0 +1,18 @@ +using Microsoft.AspNetCore.Components.Authorization; + +namespace BlazorAuth { + public static class AuthHelper { + internal static string? GetUserName(AuthenticationStateProvider provider) { + var stateTask = provider.GetAuthenticationStateAsync(); + + if (!stateTask.IsCompletedSuccessfully) + throw new InvalidOperationException("Authentication state is not ready."); + + var identity = stateTask.GetAwaiter().GetResult().User.Identity; + + return identity?.IsAuthenticated == true + ? identity.Name?.Split('@')[0] + : null; + } + } +} \ No newline at end of file diff --git a/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs b/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs index fe1e727..55d5a9a 100644 --- a/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs +++ b/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs @@ -1,19 +1,21 @@ using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Web; +using Microsoft.AspNetCore.Components.Authorization; using System.Collections.Generic; namespace BlazorAuth { public class CustomConnectionStringProvider : IDataSourceWizardConnectionStringsProvider { - private string userName; + private readonly AuthenticationStateProvider authenticationStateProvider; private Dictionary connectionStrings = new Dictionary(); - public CustomConnectionStringProvider(string userName) { - this.userName = userName; + public CustomConnectionStringProvider(AuthenticationStateProvider authenticationStateProvider) { + this.authenticationStateProvider = authenticationStateProvider; connectionStrings.Add("NorthwindConnectionString", @"XpoProvider=SQLite; Data Source=App_Data/nwind.db;"); connectionStrings.Add("CarsXtraSchedulingConnectionString", @"XpoProvider=SQLite;Data Source=App_Data/CarsDB.db;"); } public Dictionary GetConnectionDescriptions() { + var userName = AuthHelper.GetUserName(authenticationStateProvider); var connections = new Dictionary(); if (userName == "admin") { diff --git a/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs b/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs index ee5e368..210ed9f 100644 --- a/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs +++ b/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs @@ -1,18 +1,18 @@ using DevExpress.DataAccess.Sql; using DevExpress.Xpo.DB; -using Microsoft.AspNetCore.Http; -using System.Linq; +using Microsoft.AspNetCore.Components.Authorization; namespace BlazorAuth { public class CustomDBSchemaProvider : DBSchemaProviderEx { - private string userName; + private readonly AuthenticationStateProvider authenticationStateProvider; - public CustomDBSchemaProvider(string userName) : base() { - this.userName = userName; + public CustomDBSchemaProvider(AuthenticationStateProvider authenticationStateProvider) : base() { + this.authenticationStateProvider = authenticationStateProvider; } public override DBTable[] GetTables(SqlDataConnection connection, params string[] tableList) { var result = base.GetTables(connection, tableList); + var userName = AuthHelper.GetUserName(authenticationStateProvider); if (userName == "admin") { return result; diff --git a/CS/BlazorAuth/Code/CustomDashboardStorage.cs b/CS/BlazorAuth/Code/CustomDashboardStorage.cs index 319189a..28ec445 100644 --- a/CS/BlazorAuth/Code/CustomDashboardStorage.cs +++ b/CS/BlazorAuth/Code/CustomDashboardStorage.cs @@ -1,21 +1,19 @@ using DevExpress.DashboardWeb; -using Microsoft.AspNetCore.Hosting; -using System.Collections.Generic; -using System.IO; -using System.Linq; +using Microsoft.AspNetCore.Components.Authorization; using System.Xml.Linq; namespace BlazorAuth { public class CustomDashboardStorage : IEditableDashboardStorage { - private string userName; + private readonly AuthenticationStateProvider authenticationStateProvider; private string dashboardStorageFolder; - public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, string userName) { - this.userName = userName; + public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, AuthenticationStateProvider authenticationStateProvider) { + this.authenticationStateProvider = authenticationStateProvider; this.dashboardStorageFolder = hostingEnvironment.ContentRootFileProvider.GetFileInfo("App_Data/Dashboards").PhysicalPath; } public IEnumerable GetAvailableDashboardsInfo() { + var userName = AuthHelper.GetUserName(authenticationStateProvider); var dashboardInfos = new List(); var files = Directory.GetFiles(dashboardStorageFolder, "*.xml"); @@ -41,6 +39,8 @@ public XDocument LoadDashboard(string dashboardID) { } public string AddDashboard(XDocument dashboard, string dashboardName) { + var userName = AuthHelper.GetUserName(authenticationStateProvider); + if (string.IsNullOrEmpty(userName) || userName != "admin") throw new System.ApplicationException("You are not authorized to add dashboards."); @@ -52,6 +52,8 @@ public string AddDashboard(XDocument dashboard, string dashboardName) { } public void SaveDashboard(string dashboardID, XDocument dashboard) { + var userName = AuthHelper.GetUserName(authenticationStateProvider); + if (string.IsNullOrEmpty(userName) || userName != "admin") throw new System.ApplicationException("You are not authorized to save dashboards."); diff --git a/CS/BlazorAuth/Code/CustomDataSourceStorage.cs b/CS/BlazorAuth/Code/CustomDataSourceStorage.cs index 13aa0ae..ed97335 100644 --- a/CS/BlazorAuth/Code/CustomDataSourceStorage.cs +++ b/CS/BlazorAuth/Code/CustomDataSourceStorage.cs @@ -2,21 +2,20 @@ using DevExpress.DashboardWeb; using DevExpress.DataAccess.Json; using DevExpress.DataAccess.Sql; -using System.Collections.Generic; -using System.Linq; +using Microsoft.AspNetCore.Components.Authorization; using System.Xml.Linq; namespace BlazorAuth { public class CustomDataSourceStorage : IDataSourceStorage { - private string userName; + private readonly AuthenticationStateProvider authenticationStateProvider; private Dictionary documents = new Dictionary(); private const string sqlDataSourceId1 = "SQL Data Source (Northwind)"; private const string sqlDataSourceId2 = "SQL Data Source (CarsXtraScheduling)"; private const string jsonDataSourceId = "JSON Data Source"; - public CustomDataSourceStorage(string userName) { - this.userName = userName; + public CustomDataSourceStorage(AuthenticationStateProvider authenticationStateProvider) { + this.authenticationStateProvider = authenticationStateProvider; DashboardSqlDataSource sqlDataSource1 = new DashboardSqlDataSource(sqlDataSourceId1, "NorthwindConnectionString"); SelectQuery query1 = SelectQueryFluentBuilder @@ -55,6 +54,8 @@ public XDocument GetDataSource(string dataSourceID) { } public IEnumerable GetDataSourcesID() { + var userName = AuthHelper.GetUserName(authenticationStateProvider); + if (userName == "admin") { return documents.Keys; } else if (userName == "user") { diff --git a/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs b/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs index e15ea10..4d2b97c 100644 --- a/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs +++ b/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs @@ -1,23 +1,26 @@ using DevExpress.DashboardWeb; +using Microsoft.AspNetCore.Components.Authorization; namespace BlazorAuth { public class MultiTenantDashboardConfigurator : DashboardConfigurator { - private string? userName; + public MultiTenantDashboardConfigurator( + AuthenticationStateProvider authenticationStateProvider, + CustomConnectionStringProvider connectionStrings, + CustomDataSourceStorage dataSources, + CustomDashboardStorage dashboards, + CustomDBSchemaProvider schema) { - public MultiTenantDashboardConfigurator(IWebHostEnvironment hostingEnvironment, IHttpContextAccessor contextAccessor) { - string? identityName = contextAccessor.HttpContext?.User.Identity?.Name; - userName = identityName?.Substring(0, identityName.IndexOf("@")); + SetConnectionStringsProvider(connectionStrings); + SetDataSourceStorage(dataSources); + SetDashboardStorage(dashboards); + SetDBSchemaProvider(schema); - SetConnectionStringsProvider(new CustomConnectionStringProvider(userName)); - SetDataSourceStorage(new CustomDataSourceStorage(userName)); - SetDashboardStorage(new CustomDashboardStorage(hostingEnvironment, userName)); - SetDBSchemaProvider(new CustomDBSchemaProvider(userName)); + VerifyClientTrustLevel += (s, e) => { + var userName = AuthHelper.GetUserName(authenticationStateProvider); - VerifyClientTrustLevel += MultiTenantDashboardConfigurator_VerifyClientTrustLevel; - } - private void MultiTenantDashboardConfigurator_VerifyClientTrustLevel(object sender, VerifyClientTrustLevelEventArgs e) { - if (string.IsNullOrEmpty(userName) || userName == "guest") - e.ClientTrustLevel = ClientTrustLevel.Restricted; + if (string.IsNullOrEmpty(userName) || userName == "guest") + e.ClientTrustLevel = ClientTrustLevel.Restricted; + }; } } } \ No newline at end of file diff --git a/CS/BlazorAuth/Pages/Dashboard.razor b/CS/BlazorAuth/Pages/Dashboard.razor index 35a868c..fc01b20 100644 --- a/CS/BlazorAuth/Pages/Dashboard.razor +++ b/CS/BlazorAuth/Pages/Dashboard.razor @@ -1,19 +1,14 @@ @page "/dashboard" @inject AuthenticationStateProvider AuthStateProvider - - + @code { private WorkingMode workingMode; protected override async Task OnInitializedAsync() { - AuthenticationState authState = await AuthStateProvider.GetAuthenticationStateAsync(); - if (authState.User.Identity is not null) { - var identityName = authState.User.Identity.Name; - var userName = identityName?.Substring(0, identityName.IndexOf("@")); + var userName = AuthHelper.GetUserName(AuthStateProvider); - workingMode = (userName == "admin" || userName == "user") ? WorkingMode.Designer : WorkingMode.ViewerOnly; - } + workingMode = (userName == "admin" || userName == "user") ? WorkingMode.Designer : WorkingMode.ViewerOnly; } } \ No newline at end of file diff --git a/CS/BlazorAuth/Program.cs b/CS/BlazorAuth/Program.cs index 44d4f1e..16cd2ac 100644 --- a/CS/BlazorAuth/Program.cs +++ b/CS/BlazorAuth/Program.cs @@ -2,7 +2,7 @@ using BlazorAuth.Areas.Identity; using BlazorAuth.Data; using DevExpress.AspNetCore; -using DevExpress.DashboardAspNetCore; +using DevExpress.DashboardBlazor; using DevExpress.DashboardWeb; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; @@ -16,21 +16,24 @@ // Add services to the container. builder.Services.AddDbContext(options => - options.UseSqlServer( - configuration.GetConnectionString("DefaultConnection"))); + options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"))); builder.Services.AddDefaultIdentity(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores(); builder.Services.AddDevExpressControls(); builder.Services.AddRazorPages(); -builder.Services.AddHttpContextAccessor(); - +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddServerSideBlazor(); builder.Services.AddScoped>(); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); +builder.Services.AddDevExpressBlazorDashboard(); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -47,13 +50,11 @@ app.UseStaticFiles(); app.UseDevExpressControls(); app.UseRouting(); -app.MapDashboardRoute("api/dashboard", "DefaultDashboard"); app.UseAuthentication(); app.UseAuthorization(); -app.MapControllers(); app.MapBlazorHub(); app.MapFallbackToPage("/_Host"); -app.Run(); +app.Run(); \ No newline at end of file diff --git a/README.md b/README.md index fc38f89..ab443f1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This example shows how to configure the Dashboard component for Blazor so that it works in the multi-user environment. -You can identify a user in the ASP.NET Core's `HttpContext.User` and return the user-specific content. +The example uses ASP.NET Core Identity for authentication and Blazor's `AuthenticationStateProvider` to identify the current user. It configures dashboard storage, data sources, connection strings, and schema access based on that user's identity. ![](web-dashboard-blazor-auth.png) @@ -16,7 +16,7 @@ You can identify a user in the ASP.NET Core's `HttpContext.User` and return the This example was created based on the **Blazor Server App** Visual Studio template with the *Authentication Type = 'Individual Accounts'* setting (see [Secure ASP.NET Core Blazor Server apps](https://docs.microsoft.com/en-us/aspnet/core/blazor/security/server/?view=aspnetcore-5.0&tabs=visual-studio)). The example uses the [ASP.NET Core's Identity mechanism](https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-5.0&tabs=visual-studio) for authentication. For this reason, you need to follow the steps below to initialize the Identity database and register required user accounts in it: -1) Apply the `00000000000000_CreateIdentitySchema` migration to the `aspnet-BlazorAuth-9F8F37E8-C7E8-4D29-BFE6-47204A65FA44` database. You can run the following command in the Package Manager Console for this purpose: +1) Apply the `00000000000000_CreateIdentitySchema` migration to the `aspnet-BlazorAuth-...` database. You can run the following command in the Package Manager Console for this purpose: ``` PM> Update-Database @@ -39,7 +39,9 @@ If the current accounts do not exist, go to the **Register** page (`Identity/Acc ## Example Overview -You can limit access to sensitive information depending on the current user's ID. The [MultiTenantDashboardConfigurator](./CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs) class is an entry point for configuring all providers. Every custom store/provider reads the `IHttpContextAccessor.HttpContext.User.Identity`. In the `MultiTenantDashboardConfigurator` class, use the standard [IHttpContextAccessor](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-context) with dependency injection to access the HTTP context and pass the retrieved user name to Dashboard providers listed above. This class is registered as a *scoped* DI service in the [Program.cs](./CS/BlazorAuth/Program.cs) file. +The [MultiTenantDashboardConfigurator](./CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs) class configures user-specific dashboard storage and data providers. It is registered as a scoped DI service in [Program.cs](./CS/BlazorAuth/Program.cs). + +In [Dashboard.razor](./CS/BlazorAuth/Pages/Dashboard.razor), the component's `OnInitializedAsync` method retrieves the current authentication state and configures the working mode based on the user name. The configurator and each custom store/provider extracts the user name through DI. When the application starts, you see the [Index](./CS/BlazorAuth/Pages/Index.razor) view where you can select a user. Use the **Log in** link for this purpose. Below is a table that illustrates the user IDs and their associated rights in this example: From 7ac625b90576f1d06f7329176d2ff21cbb4d6d12 Mon Sep 17 00:00:00 2001 From: DevExpressExampleBot Date: Tue, 10 Nov 2026 14:23:43 +0400 Subject: [PATCH 2/4] README auto update [skip ci] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fc38f89..46022e8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/390294928/26.1.5%2B) +![](https://img.shields.io/endpoint?url=https://codecentral.devexpress.com/api/v1/VersionRange/390294928/26.2.1%2B) [![](https://img.shields.io/badge/Open_in_DevExpress_Support_Center-FF7200?style=flat-square&logo=DevExpress&logoColor=white)](https://supportcenter.devexpress.com/ticket/details/T1017576) [![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183) [![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives) From ecfafad7322a0799f35caf5b0b27061f116ee3ac Mon Sep 17 00:00:00 2001 From: AlexanderKovalenko Date: Tue, 10 Nov 2026 14:35:43 +0400 Subject: [PATCH 3/4] Update BlazorAuth.csproj --- CS/BlazorAuth/BlazorAuth.csproj | 89 +-------------------------------- 1 file changed, 2 insertions(+), 87 deletions(-) diff --git a/CS/BlazorAuth/BlazorAuth.csproj b/CS/BlazorAuth/BlazorAuth.csproj index 30c2e77..ae4fbd0 100644 --- a/CS/BlazorAuth/BlazorAuth.csproj +++ b/CS/BlazorAuth/BlazorAuth.csproj @@ -5,94 +5,9 @@ enable enable - - - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Blazor.Resources.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.Blazor.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.AspNetCore.Common.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.AspNetCore.Core.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Web.Resources.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Charts.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.AspNetCore.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.Web.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Dashboard.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Data.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.DataAccess.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.CodeParser.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Map.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Office.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Drawing.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.PivotGrid.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Pdf.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Printing.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.RichEdit.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Xpo.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.XtraReports.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.XtraCharts.v26.2.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.XtraGauges.v26.2.Core.dll - - - C:\Work\dxvcs\Bin\NETCore\DevExpress.Sparkline.v26.2.Core.dll - - - - - - - - - - - - + + From 493f6e0c18a5dd0de9cb33ea7a0e3a6d604ce147 Mon Sep 17 00:00:00 2001 From: Kochkin Date: Wed, 11 Nov 2026 00:07:28 +0400 Subject: [PATCH 4/4] refactoring - file path validation for dashboard storage - delete DashboardController - bump dependencies - AuthenticationStateProvider usage refactoring --- CS/BlazorAuth/BlazorAuth.csproj | 19 +++--- .../Code/CustomConnectionStringProvider.cs | 12 ++-- CS/BlazorAuth/Code/CustomDBSchemaProvider.cs | 11 ++-- CS/BlazorAuth/Code/CustomDashboardStorage.cs | 62 +++++++++++-------- CS/BlazorAuth/Code/CustomDataSourceStorage.cs | 33 +++++----- CS/BlazorAuth/Code/DashboardUserProvider.cs | 35 +++++++++++ .../Code/MultiTenantDashboardConfigurator.cs | 23 +++---- .../Controllers/DefaultDashboardController.cs | 11 ---- CS/BlazorAuth/Pages/Dashboard.razor | 4 +- CS/BlazorAuth/Program.cs | 11 ++-- 10 files changed, 127 insertions(+), 94 deletions(-) create mode 100644 CS/BlazorAuth/Code/DashboardUserProvider.cs delete mode 100644 CS/BlazorAuth/Controllers/DefaultDashboardController.cs diff --git a/CS/BlazorAuth/BlazorAuth.csproj b/CS/BlazorAuth/BlazorAuth.csproj index ae4fbd0..0c92673 100644 --- a/CS/BlazorAuth/BlazorAuth.csproj +++ b/CS/BlazorAuth/BlazorAuth.csproj @@ -6,16 +6,15 @@ enable - - - - - - - - - + + + + + + + + + - \ No newline at end of file diff --git a/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs b/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs index 55d5a9a..08e05b8 100644 --- a/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs +++ b/CS/BlazorAuth/Code/CustomConnectionStringProvider.cs @@ -1,21 +1,19 @@ using DevExpress.DataAccess.ConnectionParameters; using DevExpress.DataAccess.Web; -using Microsoft.AspNetCore.Components.Authorization; -using System.Collections.Generic; namespace BlazorAuth { public class CustomConnectionStringProvider : IDataSourceWizardConnectionStringsProvider { - private readonly AuthenticationStateProvider authenticationStateProvider; - private Dictionary connectionStrings = new Dictionary(); + readonly IDashboardUserProvider dashboardUserProvider; + readonly Dictionary connectionStrings = new Dictionary(); - public CustomConnectionStringProvider(AuthenticationStateProvider authenticationStateProvider) { - this.authenticationStateProvider = authenticationStateProvider; + public CustomConnectionStringProvider(IDashboardUserProvider dashboardUserProvider) { + this.dashboardUserProvider = dashboardUserProvider; connectionStrings.Add("NorthwindConnectionString", @"XpoProvider=SQLite; Data Source=App_Data/nwind.db;"); connectionStrings.Add("CarsXtraSchedulingConnectionString", @"XpoProvider=SQLite;Data Source=App_Data/CarsDB.db;"); } public Dictionary GetConnectionDescriptions() { - var userName = AuthHelper.GetUserName(authenticationStateProvider); + var userName = dashboardUserProvider.GetUserName(); var connections = new Dictionary(); if (userName == "admin") { diff --git a/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs b/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs index 210ed9f..0661ea2 100644 --- a/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs +++ b/CS/BlazorAuth/Code/CustomDBSchemaProvider.cs @@ -1,25 +1,24 @@ using DevExpress.DataAccess.Sql; using DevExpress.Xpo.DB; -using Microsoft.AspNetCore.Components.Authorization; namespace BlazorAuth { public class CustomDBSchemaProvider : DBSchemaProviderEx { - private readonly AuthenticationStateProvider authenticationStateProvider; + private readonly IDashboardUserProvider dashboardUserProvider; - public CustomDBSchemaProvider(AuthenticationStateProvider authenticationStateProvider) : base() { - this.authenticationStateProvider = authenticationStateProvider; + public CustomDBSchemaProvider(IDashboardUserProvider dashboardUserProvider) : base() { + this.dashboardUserProvider = dashboardUserProvider; } public override DBTable[] GetTables(SqlDataConnection connection, params string[] tableList) { var result = base.GetTables(connection, tableList); - var userName = AuthHelper.GetUserName(authenticationStateProvider); + var userName = dashboardUserProvider.GetUserName(); if (userName == "admin") { return result; } else if (userName == "user") { return result.Where(t => t.Name == "Cars").ToArray(); } else { - return new DBTable[0]; + return []; } } } diff --git a/CS/BlazorAuth/Code/CustomDashboardStorage.cs b/CS/BlazorAuth/Code/CustomDashboardStorage.cs index 28ec445..0e30271 100644 --- a/CS/BlazorAuth/Code/CustomDashboardStorage.cs +++ b/CS/BlazorAuth/Code/CustomDashboardStorage.cs @@ -1,27 +1,28 @@ -using DevExpress.DashboardWeb; -using Microsoft.AspNetCore.Components.Authorization; -using System.Xml.Linq; +using System.Xml.Linq; +using DevExpress.DashboardWeb; +using DevExpress.Security; namespace BlazorAuth { public class CustomDashboardStorage : IEditableDashboardStorage { - private readonly AuthenticationStateProvider authenticationStateProvider; - private string dashboardStorageFolder; + readonly IDashboardUserProvider dashboardUserProvider; + string DashboardStorageFolder { get; } - public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, AuthenticationStateProvider authenticationStateProvider) { - this.authenticationStateProvider = authenticationStateProvider; - this.dashboardStorageFolder = hostingEnvironment.ContentRootFileProvider.GetFileInfo("App_Data/Dashboards").PhysicalPath; + public CustomDashboardStorage(IWebHostEnvironment hostingEnvironment, IDashboardUserProvider dashboardUserProvider) { + this.dashboardUserProvider = dashboardUserProvider; + this.DashboardStorageFolder = hostingEnvironment.ContentRootFileProvider.GetFileInfo("App_Data/Dashboards").PhysicalPath! + ?? throw new InvalidOperationException("Dashboard storage dashboardPath is not available."); } public IEnumerable GetAvailableDashboardsInfo() { - var userName = AuthHelper.GetUserName(authenticationStateProvider); + var userName = dashboardUserProvider.GetUserName(); var dashboardInfos = new List(); - var files = Directory.GetFiles(dashboardStorageFolder, "*.xml"); + var files = Directory.GetFiles(DashboardStorageFolder, "*.xml"); - foreach (var item in files) { + foreach(var item in files) { var name = Path.GetFileNameWithoutExtension(item); - if (!string.IsNullOrEmpty(userName) && name.EndsWith(userName, System.StringComparison.InvariantCultureIgnoreCase)) + if(!string.IsNullOrEmpty(userName) && name.EndsWith(userName, System.StringComparison.InvariantCultureIgnoreCase)) dashboardInfos.Add(new DashboardInfo() { ID = name, Name = name }); } @@ -29,9 +30,11 @@ public IEnumerable GetAvailableDashboardsInfo() { } public XDocument LoadDashboard(string dashboardID) { - if (GetAvailableDashboardsInfo().Any(di => di.Name == dashboardID)) { - var path = Path.Combine(dashboardStorageFolder, dashboardID + ".xml"); - var content = File.ReadAllText(path); + if(GetAvailableDashboardsInfo().Any(di => di.Name == dashboardID)) { + var dashboardPath = Path.GetFullPath(Path.Combine(DashboardStorageFolder, dashboardID + ".xml")); + ValidateDashboardPath(dashboardPath); + + var content = File.ReadAllText(dashboardPath); return XDocument.Parse(content); } else { throw new System.ApplicationException("You are not authorized to view this dashboard."); @@ -39,27 +42,34 @@ public XDocument LoadDashboard(string dashboardID) { } public string AddDashboard(XDocument dashboard, string dashboardName) { - var userName = AuthHelper.GetUserName(authenticationStateProvider); - - if (string.IsNullOrEmpty(userName) || userName != "admin") + var userName = dashboardUserProvider.GetUserName(); + + if(string.IsNullOrEmpty(userName) || userName != "admin") throw new System.ApplicationException("You are not authorized to add dashboards."); - var path = Path.Combine(dashboardStorageFolder, dashboardName + "_" + userName + ".xml"); + var dashboardPath = Path.GetFullPath(Path.Combine(DashboardStorageFolder, dashboardName + "_" + userName + ".xml")); + ValidateDashboardPath(dashboardPath); - File.WriteAllText(path, dashboard.ToString()); + File.WriteAllText(dashboardPath, dashboard.ToString()); - return Path.GetFileNameWithoutExtension(path); + return Path.GetFileNameWithoutExtension(dashboardPath); } public void SaveDashboard(string dashboardID, XDocument dashboard) { - var userName = AuthHelper.GetUserName(authenticationStateProvider); - - if (string.IsNullOrEmpty(userName) || userName != "admin") + var userName = dashboardUserProvider.GetUserName(); + + if(string.IsNullOrEmpty(userName) || userName != "admin") throw new System.ApplicationException("You are not authorized to save dashboards."); - var path = Path.Combine(dashboardStorageFolder, dashboardID + ".xml"); + var dashboardPath = Path.GetFullPath(Path.Combine(DashboardStorageFolder, dashboardID + ".xml")); + ValidateDashboardPath(dashboardPath); + + File.WriteAllText(dashboardPath, dashboard.ToString()); + } - File.WriteAllText(path, dashboard.ToString()); + void ValidateDashboardPath(string dashboardPath) { + if(!SafePath.IsRooted(DashboardStorageFolder, dashboardPath)) + throw new InvalidOperationException("Invalid dashboard folder dashboardPath"); } } } \ No newline at end of file diff --git a/CS/BlazorAuth/Code/CustomDataSourceStorage.cs b/CS/BlazorAuth/Code/CustomDataSourceStorage.cs index ed97335..bf7044e 100644 --- a/CS/BlazorAuth/Code/CustomDataSourceStorage.cs +++ b/CS/BlazorAuth/Code/CustomDataSourceStorage.cs @@ -1,21 +1,20 @@ -using DevExpress.DashboardCommon; +using System.Xml.Linq; +using DevExpress.DashboardCommon; using DevExpress.DashboardWeb; using DevExpress.DataAccess.Json; using DevExpress.DataAccess.Sql; -using Microsoft.AspNetCore.Components.Authorization; -using System.Xml.Linq; namespace BlazorAuth { public class CustomDataSourceStorage : IDataSourceStorage { - private readonly AuthenticationStateProvider authenticationStateProvider; - private Dictionary documents = new Dictionary(); + readonly IDashboardUserProvider dashboardUserProvider; + readonly Dictionary documents = new Dictionary(); - private const string sqlDataSourceId1 = "SQL Data Source (Northwind)"; - private const string sqlDataSourceId2 = "SQL Data Source (CarsXtraScheduling)"; - private const string jsonDataSourceId = "JSON Data Source"; + const string sqlDataSourceId1 = "SQL Data Source (Northwind)"; + const string sqlDataSourceId2 = "SQL Data Source (CarsXtraScheduling)"; + const string jsonDataSourceId = "JSON Data Source"; - public CustomDataSourceStorage(AuthenticationStateProvider authenticationStateProvider) { - this.authenticationStateProvider = authenticationStateProvider; + public CustomDataSourceStorage(IDashboardUserProvider dashboardUserProvider) { + this.dashboardUserProvider = dashboardUserProvider; DashboardSqlDataSource sqlDataSource1 = new DashboardSqlDataSource(sqlDataSourceId1, "NorthwindConnectionString"); SelectQuery query1 = SelectQueryFluentBuilder @@ -46,7 +45,7 @@ public CustomDataSourceStorage(AuthenticationStateProvider authenticationStatePr } public XDocument GetDataSource(string dataSourceID) { - if (GetDataSourcesID().Contains(dataSourceID)) { + if(GetDataSourcesID().Contains(dataSourceID)) { return documents[dataSourceID]; } else { throw new System.ApplicationException("You are not authorized to use this datasource."); @@ -54,14 +53,14 @@ public XDocument GetDataSource(string dataSourceID) { } public IEnumerable GetDataSourcesID() { - var userName = AuthHelper.GetUserName(authenticationStateProvider); - - if (userName == "admin") { + var userName = dashboardUserProvider.GetUserName(); + + if(userName == "admin") { return documents.Keys; - } else if (userName == "user") { - return new string[] { sqlDataSourceId2 }; + } else if(userName == "user") { + return [sqlDataSourceId2]; } else { - return new string[0]; + return []; } } } diff --git a/CS/BlazorAuth/Code/DashboardUserProvider.cs b/CS/BlazorAuth/Code/DashboardUserProvider.cs new file mode 100644 index 0000000..5a37719 --- /dev/null +++ b/CS/BlazorAuth/Code/DashboardUserProvider.cs @@ -0,0 +1,35 @@ +using Microsoft.AspNetCore.Components.Authorization; + +namespace BlazorAuth { + public interface IDashboardUserProvider { + Task GetUserNameAsync(); + string? GetUserName(); + } + + public class DashboardUserProvider : IDashboardUserProvider { + readonly AuthenticationStateProvider authProvider; + + public DashboardUserProvider(AuthenticationStateProvider provider) { + this.authProvider = provider; + } + + public string? GetUserName() { + var stateTask = authProvider.GetAuthenticationStateAsync(); + if(!stateTask.IsCompletedSuccessfully) + throw new InvalidOperationException("Authentication state is not ready."); + + return ReadName(stateTask.Result); + } + + public async Task GetUserNameAsync() { + var state = await authProvider.GetAuthenticationStateAsync(); + + return ReadName(state); + } + + static string? ReadName(AuthenticationState state) { + var identity = state.User.Identity; + return identity?.IsAuthenticated == true ? identity.Name?.Split('@')[0] : null; + } + } +} \ No newline at end of file diff --git a/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs b/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs index 4d2b97c..6a8ff02 100644 --- a/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs +++ b/CS/BlazorAuth/Code/MultiTenantDashboardConfigurator.cs @@ -1,22 +1,23 @@ using DevExpress.DashboardWeb; -using Microsoft.AspNetCore.Components.Authorization; +using DevExpress.DataAccess.Sql; +using DevExpress.DataAccess.Web; namespace BlazorAuth { public class MultiTenantDashboardConfigurator : DashboardConfigurator { public MultiTenantDashboardConfigurator( - AuthenticationStateProvider authenticationStateProvider, - CustomConnectionStringProvider connectionStrings, - CustomDataSourceStorage dataSources, - CustomDashboardStorage dashboards, - CustomDBSchemaProvider schema) { + IDashboardUserProvider dashboardUserProvider, + IDataSourceWizardConnectionStringsProvider connectionStringsProvider, + IDataSourceStorage dataSourceStorage, + IEditableDashboardStorage dashboardStorage, + IDBSchemaProviderEx dbSchemaProvider) { - SetConnectionStringsProvider(connectionStrings); - SetDataSourceStorage(dataSources); - SetDashboardStorage(dashboards); - SetDBSchemaProvider(schema); + SetConnectionStringsProvider(connectionStringsProvider); + SetDataSourceStorage(dataSourceStorage); + SetDashboardStorage(dashboardStorage); + SetDBSchemaProvider(dbSchemaProvider); VerifyClientTrustLevel += (s, e) => { - var userName = AuthHelper.GetUserName(authenticationStateProvider); + var userName = dashboardUserProvider.GetUserName(); if (string.IsNullOrEmpty(userName) || userName == "guest") e.ClientTrustLevel = ClientTrustLevel.Restricted; diff --git a/CS/BlazorAuth/Controllers/DefaultDashboardController.cs b/CS/BlazorAuth/Controllers/DefaultDashboardController.cs deleted file mode 100644 index b6c1b4f..0000000 --- a/CS/BlazorAuth/Controllers/DefaultDashboardController.cs +++ /dev/null @@ -1,11 +0,0 @@ -using DevExpress.DashboardAspNetCore; -using DevExpress.DashboardWeb; -using Microsoft.AspNetCore.DataProtection; - -namespace BlazorAuth.Controllers { - public class DefaultDashboardController : DashboardController { - public DefaultDashboardController(DashboardConfigurator configurator, IDataProtectionProvider? dataProtectionProvider = null) - : base(configurator, dataProtectionProvider) { - } - } -} \ No newline at end of file diff --git a/CS/BlazorAuth/Pages/Dashboard.razor b/CS/BlazorAuth/Pages/Dashboard.razor index fc01b20..50c5dd2 100644 --- a/CS/BlazorAuth/Pages/Dashboard.razor +++ b/CS/BlazorAuth/Pages/Dashboard.razor @@ -1,5 +1,5 @@ @page "/dashboard" -@inject AuthenticationStateProvider AuthStateProvider +@inject IDashboardUserProvider DashboardUserProvider @@ -7,7 +7,7 @@ private WorkingMode workingMode; protected override async Task OnInitializedAsync() { - var userName = AuthHelper.GetUserName(AuthStateProvider); + var userName = await DashboardUserProvider.GetUserNameAsync(); workingMode = (userName == "admin" || userName == "user") ? WorkingMode.Designer : WorkingMode.ViewerOnly; } diff --git a/CS/BlazorAuth/Program.cs b/CS/BlazorAuth/Program.cs index 16cd2ac..371f781 100644 --- a/CS/BlazorAuth/Program.cs +++ b/CS/BlazorAuth/Program.cs @@ -4,6 +4,8 @@ using DevExpress.AspNetCore; using DevExpress.DashboardBlazor; using DevExpress.DashboardWeb; +using DevExpress.DataAccess.Sql; +using DevExpress.DataAccess.Web; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; @@ -22,10 +24,11 @@ builder.Services.AddDevExpressControls(); builder.Services.AddRazorPages(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); -builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddServerSideBlazor();