RLS Samples
This project contains demos and helper scripts for implementing
Row-Level Security (RLS) on SQL Server (2016+) and Azure SQL Database.
Getting started demos
RLS-Hospital-Builtin-Demo.sqlThis demo shows how to use RLS to control access to rows based on SQL Server's built-in security system (users and roles). Use this approach when each user has their own SQL login.
RLS-Projects-MidTier-Demo.sqlThis demo shows how to use RLS for middle-tier applications, where all database queries are done via a shared SQL login or service account. This is a common pattern for multi-tenant web apps.
Advanced scenarios
RLS-Performance-Common-Patterns.sqlThis demo demonstrates three common patterns for implementing RLS predicates. In addition, this demo shows how RLS has performance comparable to what you'd get with view-based workarounds for row-level filtering.
Details:
Row-Level Security: Performance and common patternsRLS-Middle-Tier-Apps-Demo-using_disjunctions.sqlThis demo shows how to optimize RLS query plans that have a disjunction (logical OR). This is common when you allow certain "superusers" to access all rows.
Details:
Row-Level Security for Middle-Tier Apps -- Using Disjunctions in the PredicateRLS-Query-Store.sqlThis demo shows how to optimize the performance of RLS predicates by A/B testing different configurations, and using the Query Store to measure the performance impact.
Details:
Optimizing RLS performance with the Query StoreRLS-Auto-Enable.sqlA stored procedure that automatically generates a security policy for all tables in your database.
Details:
Apply Row-Level Security to all tablesRlsTrigger.sqlA helper trigger that can automatically add a filter predicate on any newly created tables that include, for instance, a "TenantId" column.
Details:
Apply Row-Level Security automatically to newly created tables
Retired demos, left here for reference...
RLS-Middle-Tier-Apps-Demo.sqlNOTE: This demo has been superseded by the RLS-Projects-MidTier-Demo above. Use it instead! This demo creates a simple multi-tenant database for a middle-tier application, using Row-Level Security to filter rows based on a Tenant ID.
Details:
Building More Secure Middle-Tier Applications with Azure SQL Database using Row-Level SecurityRLS-Blocking-Inserts.sqlNOTE: This workaround is now obsolete due to the introduction of block predicates. We've left this here for reference, but you should really use block predicates instead. This demo shows how to implement "insert-blocking" functionality using check constraints, as an additional safeguard against mistakes in your app code.
Details:
Row-Level Security: Blocking unauthorized INSERTs