casbin-net / casbin-aspnetcore

Casbin.NET integration middleware and sample code for ASP.NET Core
https://github.com/casbin/Casbin.NET
Apache License 2.0
69 stars 21 forks source link

The options.DefaultRequestTransformerType was not found #58

Closed ZedZipDev closed 1 year ago

ZedZipDev commented 1 year ago

I have built and run samples from the sources. received the same issue as already is in the list

https://github.com/casbin-net/casbin-aspnetcore/issues/55

Then I have copied the Blazor samples to separate folder, added NuGet package and could not build because of the options.DefaultRequestTransformerType does not exist :

//Add Casbin Authorization
builder.Services.AddCasbinAuthorization(options =>
{
    options.PreferSubClaimType = ClaimTypes.Email;
    options.DefaultModelPath = Path.Combine("CasbinConfigs", "basic_model.conf");
    options.DefaultPolicyPath = Path.Combine("CasbinConfigs", "basic_policy.csv");

    // Comment line below to use the default BasicRequestTransformer
    // Note: Commenting the line means that the action methods MUST have [CasbinAuthorize()] attribute which explicitly specifies obj and policy. Otherwise authorization will be denied
    options.DefaultRequestTransformerType = typeof(KeyMatchRequestTransformer); // <---- ERROR
});

How to fix it?

casbin-bot commented 1 year ago

@sagilio @sociometry @AsakusaRinne

ZedZipDev commented 1 year ago

Any chance to fix this sample?

sagilio commented 1 year ago

Any chance to fix this sample?

Hi, if you found a build error. The last version (https://github.com/casbin-net/casbin-aspnetcore/commit/85ad73cca19ae59ff6fb94e2e1e67510bfbd4975) may be helpful for you. Could you clone it again and retry to build?

ZedZipDev commented 1 year ago

Ok, will try and let you know.

ZedZipDev commented 1 year ago

Rebuilt, run. WebApplicationSample works fine.

WebApplicationWithEfCoreSample gives an exceptions:

An unhandled exception occurred while processing the request.
MissingMethodException: Method not found: 'Void Casbin.Enforcer..ctor(Casbin.Model.IModel, Casbin.Persist.IReadOnlyAdapter, Casbin.EnforcerOptions)'.
Casbin.AspNetCore.Authorization.DefaultEnforcerProvider.GetEnforcer() in DefaultEnforcerProvider.cs, line 72

Stack Query Cookies Headers Routing
MissingMethodException: Method not found: 'Void Casbin.Enforcer..ctor(Casbin.Model.IModel, Casbin.Persist.IReadOnlyAdapter, Casbin.EnforcerOptions)'.
Casbin.AspNetCore.Authorization.DefaultEnforcerProvider.GetEnforcer() in DefaultEnforcerProvider.cs
+
        }
Casbin.AspNetCore.Authorization.DefaultEnforcerService.EnforceAsync<TRequest>(ICasbinAuthorizationContext<TRequest> context) in DefaultEnforcerService.cs
+
            var enforcer = _enforcerProvider.GetEnforcer();
Casbin.AspNetCore.Authorization.Policy.CasbinAuthorizationHandler<TRequest>.HandleRequirementAsync(AuthorizationHandlerContext context, CasbinAuthorizationRequirement requirement, ICasbinAuthorizationContext<TRequest> casbinContext) in CasbinAuthorizationHandler.cs
+
            if (await _enforcerService.EnforceAsync(casbinContext))
Microsoft.AspNetCore.Authorization.AuthorizationHandler<TRequirement, TResource>.HandleAsync(AuthorizationHandlerContext context)
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
Casbin.AspNetCore.Authorization.CasbinEvaluator.AuthorizeAsync<TRequest>(ICasbinAuthorizationContext<TRequest> casbinContext, AuthorizationPolicy policy, AuthenticateResult authenticationResult) in CasbinEvaluator.cs
+
            var result = await _authorizationService.AuthorizeAsync(casbinContext.HttpContext.User, casbinContext, policy);
Casbin.AspNetCore.Authorization.CasbinAuthorizationMiddleware<TRequest>.Invoke(HttpContext context) in CasbinAuthorizationMiddleware.cs
+
            var authorizeResult = await casbinEvaluator.AuthorizeAsync(casbinAuthorizationContext, policy, authenticateResult);
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
ZedZipDev commented 1 year ago

Also, the BlazorServer example: I have registered, logged in, clicked Test

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Access denied - BlazorWebAssemblySample.Server</title> <link rel="stylesheet" href="/Identity/lib/bootstrap/dist/css/bootstrap.css" /> <link rel="stylesheet" href="/Identity/css/site.css" /> </head> <body> <header> <nav class="
...
ZedZipDev commented 1 year ago

Yet another question. I test the WebApplicationSample. Registered user with email win32nipuh at gmail, logged in and edit the basic_policy.csv file I could not give an access to the BasicTest

# Policy below will only match using both BasicRequestTransformer and CustomRequestTransformer
p, alice@example.com, BasicTest, Get
p, win32nipuh@gmail.com, BasicTest, Get #<----in any case Access denied!!!!!

# Policy below will only match if using the CustomRequestTransformer
p, alice@example.com, /attribtest, GET
p, win32nipuh@gmail.com, /attribtest, GET #<----- it works correctly.

# Policy below will only match if using the CustomRequestTransformer
#p, alice@example.com, /home/privacy, GET

Where am I wrong? How to fix it?

ZedZipDev commented 1 year ago

Hi @sagilio , any comments? ;-)

hsluoyz commented 1 year ago

@ZedZipDev

Yet another question. I test the WebApplicationSample. Registered user with email win32nipuh at gmail, logged in and edit the basic_policy.csv file I could not give an access to the BasicTest

It has been fixed by: https://github.com/casbin-net/casbin-aspnetcore/commit/d0fa72b804822b2bcccbf2134a6768ac00a4f4cd

Policy should be:

# Policy below will only match using both BasicRequestTransformer and CustomRequestTransformer
p, alice@example.com, /Home/BasicTest, GET

# Policy below will only match if using the CustomRequestTransformer
p, alice@example.com, /attribtest, GET

# Policy below will only match if using the CustomRequestTransformer
#p, alice@example.com, /home/privacy, GET
hsluoyz commented 1 year ago

@ZedZipDev

Rebuilt, run. WebApplicationSample works fine. WebApplicationWithEfCoreSample gives an exceptions:

I didn't encounter this issue, I think you should clear and rebuild your nuget cache, or just git clone into a new folder

image

hsluoyz commented 1 year ago

@ZedZipDev

Also, the BlazorServer example: I have registered, logged in, clicked Test

This one is similar to: https://github.com/casbin-net/casbin-aspnetcore/issues/58#issuecomment-1492857520 , I think you can already fix it now

sagilio commented 1 year ago

Yet another question. I test the WebApplicationSample. Registered user with email win32nipuh at gmail, logged in and edit the basic_policy.csv file I could not give an access to the BasicTest

# Policy below will only match using both BasicRequestTransformer and CustomRequestTransformer
p, alice@example.com, BasicTest, Get
p, win32nipuh@gmail.com, BasicTest, Get #<----in any case Access denied!!!!!

# Policy below will only match if using the CustomRequestTransformer
p, alice@example.com, /attribtest, GET
p, win32nipuh@gmail.com, /attribtest, GET #<----- it works correctly.

# Policy below will only match if using the CustomRequestTransformer
#p, alice@example.com, /home/privacy, GET

Where am I wrong? How to fix it?

# Policy below will only match using both BasicRequestTransformer and CustomRequestTransformer
p, alice@example.com, BasicTest, Get

It will be matched when using the BasicRequestTransformer You can change the default transformer options.DefaultRequestTransformerType here

//Add Casbin Authorization
services.AddCasbinAuthorization(options =>
{
    options.PreferSubClaimType = ClaimTypes.Name;
    options.DefaultModelPath = Path.Combine("CasbinConfigs", "basic_model.conf");
    options.DefaultPolicyPath = Path.Combine("CasbinConfigs", "basic_policy.csv");
    options.DefaultRequestTransformerType = typeof(BasicRequestTransformer);
});

or set the transformer on attribute

[CasbinAuthorize(nameof(BasicTest), nameof(HttpMethod.Get), RequestTransformerType = typeof(BasicRequestTransformer))]
public IActionResult BasicTest()
{
    return View();
}