https://github.com/apache/casbin-aspnetcore
Casbin.NET integration middleware and sample code for ASP.NET Core
https://github.com/apache/casbin-aspnetcore
abac acl aspnet aspnetcore auth authorization casbin dotnet rbac security
Last synced: about 1 month ago
JSON representation
Casbin.NET integration middleware and sample code for ASP.NET Core
- Host: GitHub
- URL: https://github.com/apache/casbin-aspnetcore
- Owner: apache
- License: apache-2.0
- Created: 2020-07-03T00:41:13.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2026-03-23T12:21:32.000Z (3 months ago)
- Last Synced: 2026-05-02T03:12:52.727Z (about 1 month ago)
- Topics: abac, acl, aspnet, aspnetcore, auth, authorization, casbin, dotnet, rbac, security
- Language: C#
- Homepage: https://github.com/casbin/Casbin.NET
- Size: 1.14 MB
- Stars: 76
- Watchers: 8
- Forks: 20
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Casbin.AspNetCore
[](https://github.com/casbin-net/casbin-aspnetcore/actions)
[](https://coveralls.io/github/casbin-net/casbin-aspnetcore?branch=master)
[](https://github.com/casbin-net/casbin-aspnetcore/blob/master/LICENSE)
[](https://www.nuget.org/packages/Casbin.AspNetCore)
Casbin.AspNetCore is a [Casbin.NET](https://github.com/casbin/Casbin.NET) integration and extension for [ASP.NET Core](https://asp.net).
## Installation
This project is on developing, You can install the build version to try it.
```csharp
dotnet add package Casbin.AspNetCore --version
```
Or you create a `NuGet.config` file on you solution directory like this.
```xml
```
## Quick Start
You should add the service at `ConfigureServices` method and add MiddleWare at `Configure` method like this:
```csharp
public void ConfigureServices(IServiceCollection services)
{
// Other codes...
//Add Casbin Authorization
services.AddCasbinAuthorization(options =>
{
options.DefaultModelPath = "";
options.DefaultPolicyPath = "";
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// Other codes...
app.UseCasbinAuthorization();
// You can add this to support offical authorization too.
app.UseAuthorization();
// Other codes...
}
```
Now you can use the attribute like offical authorization, If you use the Basic Model, It will like this:
```csharp
[CasbinAuthorize("", "")]
public IActionResult Index()
{
return View();
}
```
## How It Works
Here is a sequence chart that can well describe the process of this middleware. In the beginning, It looks like the process of official authorization middleware. It changes in the last half part.

## Samples
Sample applications using `Casbin.AspNetCore` can be found at [sample directory](https://github.com/casbin-net/casbin-aspnetcore/tree/master/samples).
## Migrate form v0.x to v1.x
1. Check the interfaces change:
- IEnforcerProvider
```csharp
public interface IEnforcerProvider
{
// Before
public Enforcer? GetEnforcer();
// Now
public IEnforcer? GetEnforcer();
}
```
- ICasbinModelProvider
```csharp
public interface ICasbinModelProvider
{
// Before
public Model? GetModel();
// Now
public IModel? GetModel();
}
```
## Getting Help
- [Casbin.NET](https://github.com/casbin/Casbin.NET)
## License
This project is under Apache 2.0 License. See the [LICENSE](LICENSE) file for the full license text.