Skip to main content
All docs
V24.1

DashboardConfigurator.SetDBConnectionInterceptor(IDBConnectionInterceptor) Method

Registers IDBConnectionInterceptor in DashboardConfigurator.

Namespace: DevExpress.DashboardWeb

Assembly: DevExpress.Dashboard.v24.1.Web.dll

NuGet Package: DevExpress.Web.Dashboard.Common

Declaration

public void SetDBConnectionInterceptor(
    IDBConnectionInterceptor dBConnectionInterceptor
)

Parameters

Name Type Description
dBConnectionInterceptor IDBConnectionInterceptor

An IDBConnectionInterceptor object.

Remarks

Implement IDBConnectionInterceptor to intercept, modify, and/or suppress SQL operations. The list includes low-level database operations such as executing a command or setting a key-value pair in a session context. Once the connection is open, you can store values in the session context and execute any request.

Example: BI Dashboard for ASP.NET Core - Implement Row-Level Security

This example shows how to implement connection filtering in an application where users (tenants) share the same SQL Server user. Once the application connects to the database, it stores the current user ID in SESSION_CONTEXT. Security policies filter rows that shouldn’t be visible to this ID.

Refer to the following example for the complete code that implements the scenario listed above:

View Example

The following image displays the registration form where you can select a user to see the dashboard that displays filtered data based on your selection:

The following code snippet shows how to implement IDBConnectionInterceptor that filters data when the application connects to the database:

  1. Implement the IDBConnectionInterceptor interface (RLSConnectionInterceptor.cs in this example). When the database connection opens, store the current user ID in SESSION_CONTEXT, and modify queries in the Order table to filter data by user ID. This way you implement database behavior equivalent to connection filtering.

    using DevExpress.DataAccess.Sql;
    using System.Data;
    using WebDashboard.Code;
    
    namespace WebDashboard {
        public class RLSConnectionInterceptor : IDBConnectionInterceptor {
            IHttpContextAccessor contextAccessor;
            public RLSConnectionInterceptor(IHttpContextAccessor contextAccessor) {
                this.contextAccessor = contextAccessor;
            }
            public void ConnectionOpened(string sqlDataConnectionName, IDbConnection connection) {
                int employeeId = contextAccessor.GetCurrentUserId();
                using(var command = connection.CreateCommand()) {
                    command.CommandText = $"EXEC sp_set_session_context @key = N'EmployeeId', @value = {employeeId}";
                    command.ExecuteNonQuery();
                }
            }
        }
    }
    
  2. Register the interceptor in DashboardConfigurator.

    using DevExpress.DashboardCommon;
    using DevExpress.DashboardWeb;
    using DevExpress.DataAccess.Sql;
    using Microsoft.Extensions.FileProviders;
    
    namespace WebDashboard {
      public static class DashboardUtils {
        public static DashboardConfigurator CreateDashboardConfigurator(IConfiguration configuration, IFileProvider fileProvider, IHttpContextAccessor contextAccessor) {
                    DashboardConfigurator configurator = new DashboardConfigurator();
                    // ...
                    configurator.SetDBConnectionInterceptor(new RLSConnectionInterceptor(contextAccessor));
                    // ...
                    return configurator;
        } 
      } 
    }          
    
See Also