Casbin.AspNetCore is a Casbin.NET integration and extension for ASP.NET Core.
This project is on developing, You can install the build version to try it.
dotnetadd package Casbin.AspNetCore --version<build package version>Or you create a NuGet.config file on you solution directory like this.
<configuration>
<packageSources>
<addkey="nuget.org"value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>You should add the service at ConfigureServices method and add MiddleWare at Configure method like this:
publicvoidConfigureServices(IServiceCollectionservices){// Other codes...//Add Casbin Authorizationservices.AddCasbinAuthorization(options =>{options.DefaultModelPath="<model path>";options.DefaultPolicyPath="<policy path>";});}publicvoidConfigure(IApplicationBuilderapp,IWebHostEnvironmentenv){// 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>")]publicIActionResultIndex(){returnView();}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.
Sample applications using Casbin.AspNetCore can be found at sample directory.
- Check the interfaces change:
- IEnforcerProvider
publicinterfaceIEnforcerProvider{// BeforepublicEnforcer?GetEnforcer();// NowpublicIEnforcer?GetEnforcer();}- ICasbinModelProvider
publicinterfaceICasbinModelProvider{// BeforepublicModel?GetModel();// NowpublicIModel?GetModel();}This project is under Apache 2.0 License. See the LICENSE file for the full license text.
