Casbin.NET 2.7.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Casbin.NET --version 2.7.0                
NuGet\Install-Package Casbin.NET -Version 2.7.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Casbin.NET" Version="2.7.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Casbin.NET --version 2.7.0                
#r "nuget: Casbin.NET, 2.7.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Casbin.NET as a Cake Addin
#addin nuget:?package=Casbin.NET&version=2.7.0

// Install Casbin.NET as a Cake Tool
#tool nuget:?package=Casbin.NET&version=2.7.0                

Casbin.NET

Build Status Coverage Status Nuget Release Nuget Discord

News: still worry about how to write the correct Casbin policy? Casbin online editor is coming to help! Try it at: http://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:

golang java nodejs php
Casbin jCasbin node-Casbin PHP-Casbin
production-ready production-ready production-ready production-ready
python dotnet delphi rust
PyCasbin Casbin.NET Casbin4D Casbin-RS
production-ready production-ready experimental production-ready

Table of contents

Supported models

  1. ACL (Access Control List)
  2. ACL with superuser
  3. ACL without users: especially useful for systems that don't have authentication or user log-ins.
  4. 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.
  5. RBAC (Role-Based Access Control)
  6. RBAC with resource roles: both users and resources can have roles (or groups) at the same time.
  7. RBAC with domains/tenants: users can have different role sets for different domains/tenants.
  8. ABAC (Attribute-Based Access Control): syntax sugar like resource.Owner can be used to get the attribute for a resource.
  9. RESTful: supports paths like /res/*, /res/:id and HTTP methods like GET, POST, PUT, DELETE.
  10. Deny-override: both allow and deny authorizations are supported, deny overrides the allow.
  11. 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:

  1. enforce the policy in the classic {subject, object, action} form or a customized form as you defined, both allow and deny authorizations are supported.
  2. handle the storage of the access control model and its policy.
  3. manage the role-user mappings and role-role mappings (aka role hierarchy in RBAC).
  4. support built-in superuser like root or administrator. A superuser can do anything without explict permissions.
  5. 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:

  1. authentication (aka verify username and password when a user logs in)
  2. 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 (http://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

  1. 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.

  1. 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
    }
    
  2. 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.

  1. 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:

model editor

policy editor

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 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. 
.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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 2,210 10/29/2024
2.11.0 1,547 10/29/2024
2.10.0 8,117 9/3/2024
2.9.1 14,227 8/11/2024
2.9.0 13,957 6/18/2024
2.8.0 10,859 6/6/2024
2.7.1 3,065 5/30/2024
2.7.0 24,779 4/27/2024
2.6.0 858 4/22/2024
2.5.3 18,285 4/5/2024
2.5.2 411 4/5/2024
2.5.1 15,461 4/3/2024
2.5.0 19,415 3/9/2024
2.4.0 7,960 2/17/2024
2.1.1 46,823 10/4/2023
2.1.0 872 9/16/2023
2.0.3 3,551 8/27/2023
2.0.2 404 8/24/2023
2.0.1 11,189 8/4/2023
2.0.0 1,101 7/27/2023
2.0.0-preview.5 21,064 1/6/2023
2.0.0-preview.4 39,601 6/6/2022
2.0.0-preview.3 794 2/23/2022
2.0.0-preview.2 245 8/11/2021
2.0.0-preview.1 231 6/16/2021
1.14.0 7,486 6/10/2023
1.13.0 28,514 11/24/2022
1.12.2 112,061 3/11/2022
1.12.1 53,242 1/20/2022
1.12.0 74,436 11/24/2021
1.11.0 14,593 8/20/2021
1.10.1 16,813 7/22/2021
1.10.0 938 7/18/2021
1.9.0 8,139 6/29/2021
1.8.1 1,548 6/7/2021
1.8.0 1,975 5/23/2021
1.7.3 3,303 5/17/2021
1.7.2 18,983 4/23/2021
1.7.1 6,574 4/15/2021
1.7.0 6,865 4/7/2021
1.6.2 1,131 4/3/2021
1.6.1 1,004 3/31/2021
1.6.0 880 3/30/2021
1.5.2 20,495 3/23/2021
1.5.1 975 3/19/2021
1.5.0 5,113 2/12/2021
1.4.0 11,204 10/8/2020
1.3.1 14,255 7/30/2020
1.3.0 3,006 6/29/2020
1.2.7 1,617 6/13/2020
1.2.6 9,205 5/26/2020
1.2.5 9,448 4/22/2020
1.2.4 3,526 12/7/2019
1.2.3 52,216 11/22/2019
1.2.2 3,033 11/18/2019
1.2.0 8,693 7/30/2019
1.1.1 2,748 6/29/2019
1.1.0 1,693 6/20/2019