Casbin.NET integration middleware and sample code for ASP.NET Core

Clone this repo:
  1. 95d19c1 feat: add missing 13 and 14 parameter constructors for CasbinAuthorizeAttribute (#73) by Yang Luo · 10 weeks ago master v1.6.0
  2. cd57f93 feat: fix duplicate CI workflows on pull requests (#75) by Yang Luo · 10 weeks ago
  3. 4b1c0bb Updated to Casbin.NET 2.19.1 (#69) by Thor Arne Johansen · 3 months ago
  4. f526369 feat: update deprecated GitHub Actions to v4 in CI (#71) by Yang Luo · 3 months ago v1.5.0
  5. 5db5b3d feat: update Casbin.NET and EFCore adapter's dependency versions (#68) by DacongDA · 1 year, 11 months ago v1.4.0

Casbin.AspNetCore

Actions Status Coverage Status License Nuget

Casbin.AspNetCore is a Casbin.NET integration and extension for ASP.NET Core.

Installation

This project is on developing, You can install the build version to try it.

dotnet add package Casbin.AspNetCore --version <build package version>

Or you create a NuGet.config file on you solution directory like this.

<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>

Quick Start

You should add the service at ConfigureServices method and add MiddleWare at Configure method like this:

public void ConfigureServices(IServiceCollection services)
{
    // Other codes...

    //Add Casbin Authorization
    services.AddCasbinAuthorization(options =>
    {
        options.DefaultModelPath = "<model path>";
        options.DefaultPolicyPath = "<policy path>";
    });
}

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:

[CasbinAuthorize("<obj>", "<act>")]
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.

Sequence Chart of Middleware

Samples

Sample applications using Casbin.AspNetCore can be found at sample directory.

Migrate form v0.x to v1.x

  1. Check the interfaces change:
  • IEnforcerProvider
public interface IEnforcerProvider
{
    // Before
    public Enforcer? GetEnforcer();
    // Now
    public IEnforcer? GetEnforcer();
}
  • ICasbinModelProvider
public interface ICasbinModelProvider
{
    // Before
    public Model? GetModel();
    // Now
    public IModel? GetModel();
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.