Casbin.NET
2.12.0
dotnet add package Casbin.NET --version 2.12.0
NuGet\Install-Package Casbin.NET -Version 2.12.0
<PackageReference Include="Casbin.NET" Version="2.12.0" />
paket add Casbin.NET --version 2.12.0
#r "nuget: Casbin.NET, 2.12.0"
// Install Casbin.NET as a Cake Addin #addin nuget:?package=Casbin.NET&version=2.12.0 // Install Casbin.NET as a Cake Tool #tool nuget:?package=Casbin.NET&version=2.12.0
Casbin.NET
News: still worry about how to write the correct Casbin policy? Casbin online editor
is coming to help! Try it at: https://casbin.org/editor/
Casbin.NET is a powerful and efficient open-source access control library for .NET (C#) projects. It provides support for enforcing authorization based on various access control models.
All the languages supported by Casbin:
Casbin | jCasbin | node-Casbin | PHP-Casbin |
production-ready | production-ready | production-ready | production-ready |
PyCasbin | Casbin.NET | Casbin4D | Casbin-RS |
production-ready | production-ready | experimental | production-ready |
Table of contents
- Supported models
- How it works?
- Features
- Installation
- Documentation
- Online editor
- Tutorials
- Get started
- Policy management
- Policy persistence
- Policy consistence between multiple nodes
- Role manager
- Benchmarks
- Examples
- Middlewares
- Our adopters
Supported models
- ACL (Access Control List)
- ACL with superuser
- ACL without users: especially useful for systems that don't have authentication or user log-ins.
- ACL without resources: some scenarios may target for a type of resources instead of an individual resource by using permissions like
write-article
,read-log
. It doesn't control the access to a specific article or log. - RBAC (Role-Based Access Control)
- RBAC with resource roles: both users and resources can have roles (or groups) at the same time.
- RBAC with domains/tenants: users can have different role sets for different domains/tenants.
- ABAC (Attribute-Based Access Control): syntax sugar like
resource.Owner
can be used to get the attribute for a resource. - RESTful: supports paths like
/res/*
,/res/:id
and HTTP methods likeGET
,POST
,PUT
,DELETE
. - Deny-override: both allow and deny authorizations are supported, deny overrides the allow.
- Priority: the policy rules can be prioritized like firewall rules.
How it works?
In Casbin, an access control model is abstracted into a CONF file based on the PERM metamodel (Policy, Effect, Request, Matchers). So switching or upgrading the authorization mechanism for a project is just as simple as modifying a configuration. You can customize your own access control model by combining the available models. For example, you can get RBAC roles and ABAC attributes together inside one model and share one set of policy rules.
The most basic and simplest model in Casbin is ACL. ACL's model CONF is:
# Request definition
[request_definition]
r = sub, obj, act
# Policy definition
[policy_definition]
p = sub, obj, act
# Policy effect
[policy_effect]
e = some(where (p.eft == allow))
# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
An example policy for ACL model is like:
p, alice, data1, read
p, bob, data2, write
It means:
- alice can read data1
- bob can write data2
We also support multi-line mode by appending '\' in the end:
# Matchers
[matchers]
m = r.sub == p.sub && r.obj == p.obj \
&& r.act == p.act
Further more, if you are using ABAC, you can try operator in
like following in Casbin golang edition (jCasbin and Node-Casbin are not supported yet):
# Matchers
[matchers]
m = r.obj == p.obj && r.act == p.act || r.obj in ('data2', 'data3')
But you SHOULD make sure that the length of the array is MORE than 1, otherwise there will cause it to panic.
For more operators, you may take a look at govaluate
Features
What Casbin does:
- enforce the policy in the classic
{subject, object, action}
form or a customized form as you defined, both allow and deny authorizations are supported. - handle the storage of the access control model and its policy.
- manage the role-user mappings and role-role mappings (aka role hierarchy in RBAC).
- support built-in superuser like
root
oradministrator
. A superuser can do anything without explict permissions. - multiple built-in operators to support the rule matching. For example,
keyMatch
can map a resource key/foo/bar
to the pattern/foo*
.
What Casbin does NOT do:
- authentication (aka verify
username
andpassword
when a user logs in) - manage the list of users or roles. I believe it's more convenient for the project itself to manage these entities. Users usually have their passwords, and Casbin is not designed as a password container. However, Casbin stores the user-role mapping for the RBAC scenario.
Installation
dotnet add package Casbin.NET
Documentation
https://casbin.org/docs/overview
Online editor
You can also use the online editor (https://casbin.org/editor/) to write your Casbin model and policy in your web browser. It provides functionality such as syntax highlighting
and code completion
, just like an IDE for a programming language.
Tutorials
https://casbin.org/docs/tutorials
Get started
New a Casbin enforcer with a model file and a policy file:
var e = new Enforcer("path/to/model.conf", "path/to/policy.csv")
Note: you can also initialize an enforcer with policy in DB instead of file, see Persistence section for details.
Add an enforcement hook into your code right before the access happens:
var sub = "alice" // the user that wants to access a resource. var obj = "data1" // the resource that is going to be accessed. var act = "read" // the operation that the user performs on the resource. if (e.Enforce(sub, obj, act)) { // permit alice to read data1 } else { // deny the request, show an error }
Besides the static policy file, Casbin also provides API for permission management at run-time. For example, You can get all the roles assigned to a user as below:
var roles = e.GetRolesForUser("alice")
See Policy management APIs for more usage.
- Please refer to the
Casbin.UnitTest
project for more usage.
Policy management
Casbin provides two sets of APIs to manage permissions:
- Management API: the primitive API that provides full support for Casbin policy management. See here for examples.
- RBAC API: a more friendly API for RBAC. This API is a subset of Management API. The RBAC users could use this API to simplify the code. See here for examples.
We also provide a web-based UI for model management and policy management:
Policy persistence
https://casbin.org/docs/adapters
Policy consistence between multiple nodes
https://casbin.org/docs/watchers
Role manager
https://casbin.org/docs/role-managers
Benchmarks
https://casbin.org/docs/benchmark
Examples
Model | Model file | Policy file |
---|---|---|
ACL | basic_model.conf | basic_policy.csv |
ACL with superuser | basic_model_with_root.conf | basic_policy.csv |
ACL without users | basic_model_without_users.conf | basic_policy_without_users.csv |
ACL without resources | basic_model_without_resources.conf | basic_policy_without_resources.csv |
RBAC | rbac_model.conf | rbac_policy.csv |
RBAC with resource roles | rbac_model_with_resource_roles.conf | rbac_policy_with_resource_roles.csv |
RBAC with domains/tenants | rbac_model_with_domains.conf | rbac_policy_with_domains.csv |
ABAC | abac_model.conf | N/A |
RESTful | keymatch_model.conf | keymatch_policy.csv |
Deny-override | rbac_model_with_deny.conf | rbac_policy_with_deny.csv |
Priority | priority_model.conf | priority_policy.csv |
Middlewares
Authz middlewares for web frameworks: https://casbin.org/docs/middlewares
Our adopters
https://casbin.org/docs/adopters
Contributors
This project exists thanks to all the people who contribute. <p><a href="https://github.com/casbin/Casbin.NET/graphs/contributors"><img src="https://opencollective.com/casbinnet/contributors.svg?width=890&button=false"></a></p>
Backers
Thank you to all our backers! 🙏 [Become a backer]
<p><a href="https://opencollective.com/casbin#backers" target="_blank"><img src="https://opencollective.com/casbin/backers.svg?width=890"></a></p>
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
<p><a href="https://opencollective.com/casbin/sponsor/0/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/0/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/1/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/1/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/2/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/2/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/3/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/3/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/4/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/4/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/5/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/5/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/6/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/6/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/7/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/7/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/8/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/8/avatar.svg"></a> <a href="https://opencollective.com/casbin/sponsor/9/website" target="_blank"><img src="https://opencollective.com/casbin/sponsor/9/avatar.svg"></a> </p>
License
This project is licensed under the Apache 2.0 license.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 is compatible. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 is compatible. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net452 is compatible. net46 was computed. net461 is compatible. net462 is compatible. 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. |
-
.NETCoreApp 3.1
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 3.1.32)
- Microsoft.Extensions.Logging (>= 3.1.32)
- System.Memory (>= 4.5.5)
-
.NETFramework 4.5.2
- CsvHelper (>= 30.0.1)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.10.0)
- System.Memory (>= 4.5.5)
- System.ValueTuple (>= 4.5.0)
-
.NETFramework 4.6.1
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 6.0.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- System.Memory (>= 4.5.5)
-
.NETFramework 4.6.2
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 6.0.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- System.Memory (>= 4.5.5)
-
.NETStandard 2.0
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 6.0.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- System.Memory (>= 4.5.5)
-
.NETStandard 2.1
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 6.0.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- System.Memory (>= 4.5.5)
-
net5.0
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 5.0.0)
- Microsoft.Extensions.Logging (>= 5.0.0)
- System.Memory (>= 4.5.5)
-
net6.0
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 6.0.0)
- Microsoft.Extensions.Logging (>= 6.0.0)
- System.Memory (>= 4.5.5)
-
net7.0
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 7.0.0)
- Microsoft.Extensions.Logging (>= 7.0.0)
- System.Memory (>= 4.5.5)
-
net8.0
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 8.0.0)
- Microsoft.Extensions.Logging (>= 8.0.0)
- System.Memory (>= 4.5.5)
-
net9.0
- CsvHelper (>= 32.0.3)
- DotNet.Glob (>= 3.1.3)
- DynamicExpresso.Core (>= 2.16.1)
- Microsoft.Extensions.Configuration.Ini (>= 9.0.0-preview.4.24266.19)
- Microsoft.Extensions.Logging (>= 9.0.0-preview.4.24266.19)
- System.Memory (>= 4.5.5)
NuGet packages (8)
Showing the top 5 NuGet packages that depend on Casbin.NET:
Package | Downloads |
---|---|
Casbin.NET.Adapter.EFCore
Package Description |
|
Casbin.NET.Watcher.Redis
Redis watcher for Casbin.NET |
|
Casbin.AspNetCore.Abstractions
Package Description |
|
AccessSentry
Package Description |
|
Casbin.NET.Adapter.EFCore.Aleksa
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.12.0 | 453 | 10/29/2024 |
2.11.0 | 723 | 10/29/2024 |
2.10.0 | 6,996 | 9/3/2024 |
2.9.1 | 12,414 | 8/11/2024 |
2.9.0 | 13,703 | 6/18/2024 |
2.8.0 | 10,817 | 6/6/2024 |
2.7.1 | 3,052 | 5/30/2024 |
2.7.0 | 22,637 | 4/27/2024 |
2.6.0 | 838 | 4/22/2024 |
2.5.3 | 16,608 | 4/5/2024 |
2.5.2 | 406 | 4/5/2024 |
2.5.1 | 14,524 | 4/3/2024 |
2.5.0 | 19,177 | 3/9/2024 |
2.4.0 | 7,942 | 2/17/2024 |
2.1.1 | 46,553 | 10/4/2023 |
2.1.0 | 867 | 9/16/2023 |
2.0.3 | 3,545 | 8/27/2023 |
2.0.2 | 399 | 8/24/2023 |
2.0.1 | 10,773 | 8/4/2023 |
2.0.0 | 1,096 | 7/27/2023 |
2.0.0-preview.5 | 21,041 | 1/6/2023 |
2.0.0-preview.4 | 39,573 | 6/6/2022 |
2.0.0-preview.3 | 791 | 2/23/2022 |
2.0.0-preview.2 | 238 | 8/11/2021 |
2.0.0-preview.1 | 225 | 6/16/2021 |
1.14.0 | 7,223 | 6/10/2023 |
1.13.0 | 28,280 | 11/24/2022 |
1.12.2 | 111,145 | 3/11/2022 |
1.12.1 | 53,007 | 1/20/2022 |
1.12.0 | 73,975 | 11/24/2021 |
1.11.0 | 14,547 | 8/20/2021 |
1.10.1 | 16,776 | 7/22/2021 |
1.10.0 | 934 | 7/18/2021 |
1.9.0 | 8,132 | 6/29/2021 |
1.8.1 | 1,544 | 6/7/2021 |
1.8.0 | 1,971 | 5/23/2021 |
1.7.3 | 3,299 | 5/17/2021 |
1.7.2 | 18,348 | 4/23/2021 |
1.7.1 | 6,552 | 4/15/2021 |
1.7.0 | 6,861 | 4/7/2021 |
1.6.2 | 1,127 | 4/3/2021 |
1.6.1 | 1,000 | 3/31/2021 |
1.6.0 | 876 | 3/30/2021 |
1.5.2 | 20,199 | 3/23/2021 |
1.5.1 | 971 | 3/19/2021 |
1.5.0 | 5,096 | 2/12/2021 |
1.4.0 | 11,192 | 10/8/2020 |
1.3.1 | 14,186 | 7/30/2020 |
1.3.0 | 2,998 | 6/29/2020 |
1.2.7 | 1,611 | 6/13/2020 |
1.2.6 | 9,199 | 5/26/2020 |
1.2.5 | 9,439 | 4/22/2020 |
1.2.4 | 3,520 | 12/7/2019 |
1.2.3 | 52,210 | 11/22/2019 |
1.2.2 | 3,025 | 11/18/2019 |
1.2.0 | 8,504 | 7/30/2019 |
1.1.1 | 2,739 | 6/29/2019 |
1.1.0 | 1,646 | 6/20/2019 |