Asynchronous Authorization Rules with Blazor and AuthorizeView Policies #2643
Replies: 3 comments 5 replies
-
Interesting question. My first thought is that it probably isn't possible, because we don't control how Blazor interacts with the Authorize attribute, and I very much doubt they invoke the attribute asynchronously. Therefore, the when the attribute is invoked, it must provide an immediate result, before any async task would complete. That is something you could test, or look at the Blazor code to confirm. There's no doubt that the per-type rules are not intended to act on instance data as you describe. The scope of the CSLA feature is purely to operate on the business domain type plus any ambient context (like the user identity, ClientContext, etc.). What you want to do is perfectly reasonable however, so it seems worth thinking through how to cleanly solve your scenario: once some information is known about the domain instance to be created/fetched/updated/deleted/executed, what is the best way to abstract a "HasPermission" question that includes ambient context (user identity, ClientContext) and also this potentially limited information about the domain instance. I just reviewing this between meetings, so don't have an answer offhand. It is worth thinking about further for sure. |
Beta Was this translation helpful? Give feedback.
-
Added to backlog: #2801 |
Beta Was this translation helpful? Give feedback.
-
I don't think this is possible, because iirc, the aspnetcore policy scheme is really just a string. So I don't think there's a way to pass complex data like an object reference. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
My question is two-fold:
Here's the context for these questions. I have a project that I've been maintaining since the v3.x days where we created CanGetObject, CanAddObject, CanEditObject, etc. When 4.x was first introduced, there wasn't really an option to pass a dynamic parameter into the rules, and I needed the ability to hit the database to check the ID of the object to see if the user has individual permissions to view that object. Basically, a user needed to have a certain role, but also have specific permission to view a client record. Because 4.x didn't have that (or I couldn't see how to do it) I just continued to use the CanGetObject function that I was using for 3.x. So here's an example where I've used the 4.x AddObjectAuthorization with the Roles, and then call that in a CanGetObject function that I would then call specifically from my UI:
Since then, the ability to add the criteria object as an overload of the HasPermission method in 5.5.2 #1226 has opened up the ability for me to start using the built in authorization system with my own custom AuthorizationRule that would hit the DB. However, with Blazor, I now have to run my Command Object that hits the DB asynchronously, but I don't see an AuthorizationRuleAsync like I do a BusinessRuleAsync.
So, hence my first question is there a way to make an asynchronous authorization rule?
Right now my workaround is to keep using my own method converted to async:
But that leads to my second question in wanting to use the AuthorizeView in Blazor with the CslaPolicy. I obviously can't do it with my work around, but assuming there was the option for an asynchronous auth rule, is there a way to add the ability to call the other overloads of HasPermission with the CslaPolicy? Right now GetPolicy only takes the action and the object type. It would be great if it could also take the criteria objects or the business object itself (in the case of editing).
Beta Was this translation helpful? Give feedback.
All reactions