GateKeeper 2.2.1
dotnet add package GateKeeper --version 2.2.1
NuGet\Install-Package GateKeeper -Version 2.2.1
<PackageReference Include="GateKeeper" Version="2.2.1" />
<PackageVersion Include="GateKeeper" Version="2.2.1" />
<PackageReference Include="GateKeeper" />
paket add GateKeeper --version 2.2.1
#r "nuget: GateKeeper, 2.2.1"
#:package GateKeeper@2.2.1
#addin nuget:?package=GateKeeper&version=2.2.1
#tool nuget:?package=GateKeeper&version=2.2.1
<img src="https://github.com/jchristn/Gatekeeper/raw/master/assets/icon.png" width="100" height="100">
GateKeeper
A lightweight, easy-to-use roles-based access control (RBAC) library for .NET applications.
Overview
GateKeeper provides a simple yet powerful way to implement authorization in your .NET applications. Define users, roles, resources, and permissions, then authorize access attempts with a single method call.
Features
- Simple, intuitive API for managing users, roles, resources, and permissions
- SQLite-based persistence (no external database required)
- Event-driven authorization with detailed matching information
- Automatic cleanup of related records when deleting entities
- Input sanitization for security
- Built-in OpenTelemetry-compatible metrics and traces (no dependencies, near-zero cost when unobserved)
- Cross-platform support
Supported Frameworks
- .NET Standard 2.0
- .NET Standard 2.1
- .NET 8.0
- .NET 10.0
Installation
Install via NuGet Package Manager:
dotnet add package GateKeeper
Or via the Package Manager Console:
Install-Package GateKeeper
Quick Start
using GateKeeper;
// 1. Create the RBAC server (uses SQLite database)
RbacServer server = new RbacServer();
// Or specify a custom database file:
// RbacServer server = new RbacServer("myapp.db");
// 2. Create a user
User user = server.Users.Add(new User("alice"));
// 3. Create a resource
Resource resource = server.Resources.Add(new Resource("documents"));
// 4. Create a role
Role role = server.Roles.Add(new Role("editor"));
// 5. Create a permission (role + resource + operation + allow/deny)
Permission permission = server.Permissions.Add(
new Permission("editor-can-edit-documents", role, resource, "edit", true)
);
// 6. Assign the user to the role
UserRole userRole = server.UserRoles.Add(user, role);
// 7. Authorize a request
bool authorized = server.Authorize("alice", "edit", "documents");
Console.WriteLine($"Authorized: {authorized}"); // Output: Authorized: True
Core Concepts
Users
Entities that attempt to access resources. Users are assigned to one or more roles.
Roles
Groups that define a set of permissions. Users inherit permissions from their assigned roles.
Resources
Protected entities that users attempt to access (e.g., files, APIs, features).
Permissions
Rules that grant or deny a specific operation on a resource to a role.
Operations
Actions that can be performed on resources (e.g., "create", "read", "update", "delete").
API Reference
RbacServer
The main entry point for the GateKeeper library.
// Create with default database file (gatekeeper.db)
RbacServer server = new RbacServer();
// Create with custom database file
RbacServer server = new RbacServer("custom.db");
// Set default behavior when no matching permission is found
server.DefaultPermit = false; // Default: deny
// Authorize a request
bool result = server.Authorize("username", "operation", "resource");
// Authorize with metadata (passed to events)
bool result = server.Authorize("username", "operation", "resource", myMetadata);
Manager APIs
Each manager (Users, Roles, Resources, Permissions, UserRoles) provides:
| Method | Description |
|---|---|
Add(entity) |
Add a new entity |
Remove(entity) |
Remove an entity |
RemoveByName(name) |
Remove an entity by name |
All() |
Retrieve all entities |
GetFirstByName(name) |
Get an entity by name |
ExistsByName(name) |
Check if an entity exists |
Authorization Events
Subscribe to authorization events for logging, auditing, or custom logic:
server.AuthorizationEvent += (sender, args) =>
{
Console.WriteLine($"User: {args.Username}");
Console.WriteLine($"Operation: {args.Operation}");
Console.WriteLine($"Resource: {args.Resource}");
Console.WriteLine($"Authorized: {args.Authorized}");
Console.WriteLine($"Matching Entries: {args.MatchingEntries?.Count ?? 0}");
if (args.Metadata != null)
Console.WriteLine($"Metadata: {args.Metadata}");
};
Observability
GateKeeper emits metrics and traces through the .NET base class library, using a Meter and an ActivitySource both named GateKeeper. It takes no dependency on OpenTelemetry or any exporter. Subscribe from your host to collect authorization latency (end to end and per stage), allow/deny/default decisions, SQLite call latency and failures, AuthorizationEvent handler failures and backlog, management operation outcomes, and build info:
// Radiant
settings.Sources.AddMeter("GateKeeper");
settings.Sources.AddActivitySource("GateKeeper");
// or the OpenTelemetry SDK
Sdk.CreateMeterProviderBuilder().AddMeter("GateKeeper").AddOtlpExporter().Build();
Sdk.CreateTracerProviderBuilder().AddSource("GateKeeper").AddOtlpExporter().Build();
When Authorize runs inside a traced request, such as a Watson handler, its GateKeeper Authorize span nests under the request span. Usernames and query text are never recorded. See TELEMETRY.md for the full metric and span catalog, PromQL, alerts, and a dashboard layout.
Sample Application
The GateKeeperConsole project provides an interactive console application for testing GateKeeper functionality. Run it to:
- Create and manage users, roles, resources, and permissions
- Test authorization scenarios
- Explore the API interactively
cd src/GateKeeperConsole
dotnet run
Automated Tests
All test logic lives in Test.Shared (Touchstone suites) and runs identically under three hosts:
cd src
dotnet run --project Test.Automated -f net10.0 # console runner
dotnet test # xUnit and NUnit hosts
The test suite validates:
- User, role, resource, and permission management
- User-role mappings
- Authorization logic
- Default permit behavior
- Authorization events
- Cascade deletes
- Input validation
- Telemetry emission (metrics and spans for every operation, failure paths, context propagation, and safety with no listener or throwing listeners)
Project Structure
GateKeeper/
├── src/
│ ├── GateKeeper/ # Main library
│ ├── GateKeeperConsole/ # Interactive console demo
│ ├── Test.Shared/ # Shared Touchstone test suites
│ ├── Test.Automated/ # Console test runner
│ ├── Test.Xunit/ # xUnit host
│ └── Test.Nunit/ # NUnit host
├── assets/ # Icons and images
├── README.md
├── TELEMETRY.md # Metrics and spans catalog
└── LICENSE.md
Building from Source
cd src
dotnet restore
dotnet build
Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
Support
- Issues: GitHub Issues
- Discussions: GitHub Discussions
Version History
See CHANGELOG.md for a detailed version history.
v2.2.1
- Dependency maintenance: updated test dependencies (Touchstone 0.2.0, NUnit 5.0.0, NUnit3TestAdapter 6.3.0, Microsoft.NET.Test.Sdk 18.10.1); no library changes
v2.2.0
- Built-in metrics and traces through the BCL
MeterandActivitySourcenamedGateKeeper(see TELEMETRY.md) Authorizeno longer writes its SQL query to the console
v2.1.0
- Retargeted to .NET Standard 2.0, .NET Standard 2.1, .NET 8.0, and .NET 10.0
- Updated WatsonORM.Sqlite to v3.0.14
- Migrated to System.Text.Json (removed Newtonsoft.Json dependency from console projects)
- Reorganized project structure (source moved to
src/directory) - Added comprehensive automated test suite
v2.0.0
- Breaking changes and major refactor
- Content sanitization on insert and authorization evaluation
- Event handler for authorization decisions including evaluation metadata
- Automatic cleanup of subordinate objects
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- SQLitePCLRaw.bundle_e_sqlite3 (>= 3.0.5)
- System.Diagnostics.DiagnosticSource (>= 10.0.12)
- WatsonORM.Sqlite (>= 3.0.14)
-
.NETStandard 2.1
- SQLitePCLRaw.bundle_e_sqlite3 (>= 3.0.5)
- System.Diagnostics.DiagnosticSource (>= 10.0.12)
- WatsonORM.Sqlite (>= 3.0.14)
-
net10.0
- SQLitePCLRaw.bundle_e_sqlite3 (>= 3.0.5)
- WatsonORM.Sqlite (>= 3.0.14)
-
net8.0
- SQLitePCLRaw.bundle_e_sqlite3 (>= 3.0.5)
- System.Diagnostics.DiagnosticSource (>= 10.0.12)
- WatsonORM.Sqlite (>= 3.0.14)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Dependency maintenance release; updated test dependencies (Touchstone 0.2.0, NUnit 5.0.0, Microsoft.NET.Test.Sdk 18.10.1). No library API or runtime dependency changes.