Security icon indicating copy to clipboard operation
Security copied to clipboard

unable to configure Authorization policies at other than AddAuthorization

Open Tgueth opened this issue 10 years ago • 10 comments

I would to configure Authorization policies at the Configuration stage of Startup rather than ConfigureServices stage. However, when I have the AuthorizationService (IAuthorizationService passed) to Configure(), there is not an exposed method or access to options list add to it. Not clear to me why this is not allowed since web pages are not running at this stage and so Authorization is not at risk.

Second, I do not understand how to use the services.Configure<> to load the desired polices, or if that is even possible, since ConfigureAuthorization was removed.

Tgueth avatar Feb 24 '16 18:02 Tgueth

Why do you want to do it inside Configuration()? Authorization is a service, so by design it's configured inside ConfigureServices()

blowdart avatar Feb 24 '16 19:02 blowdart

Let's start with that I like the new Authorization capability and the power it provides. Kudos to open sourcing the code which is helpful.

But let me try to answer the why:

  1. Present architecture (as I see via the playing and reading through the code) requires that I do the configuration as part of the AddAuthorization() at the time the service is created. This okay but requires a fixed architecture at time of compile. Okay during early phases, but requires IIS server or application restart to implement changes, losing any existing Session and in memory information. As noted, okay at debug time but long term not best for new applications.

  2. I have about 12 policies that I add. Short term, I would like to create them in separate method where I return the list of policies to be added. While I could just pass IServiceCollection, do the work and return IServiceCollection (aka the extensions), would prefer to isolate so just returns the policies to be added. Not clear to me how this can be accomplished to add the policies at the AddAuthorization() as it does not accept a list of policies. Or am I missing something simple.

  3. Long term, I would like to pull the policy requirements information from a configuration file or even pull from a database. Could still be done in ConfigureServices, but doesn't feel right to me.

  4. And lastly, why would you care whether I configure the Services in ConfigureServices() or Configure()? From my view, ConfigureServices() is meant to add and initialize the services needed for the application and should only have configuration information requied at startup time. Any other configuration information (such as adding the policies or adding new ones) in my opinion should be in Configure(), or at least should have the option to make changes there. But that is my opinion. I can live with a requirement for service configuration to be in the service addition, but as noted above, not clear as to how I can create a list of policies and have them passed to AddAuthorization().

I will admit that I am just digging into this area. I could modify the Authorization code to my liking, but trying to live within the present and hopefully future structure for Authorization.

Tgueth avatar Feb 24 '16 22:02 Tgueth

I don't see what's wrong with pulling policies from the database inside configure services, but I'd like to know what you want inside the database. Remember you can inject into your handler.

I'm guessing that your database may contain roles or permissions that change at runtime? If so, I'd inject the permissions repo into the handler itself, and pull them out at evaluation time.

blowdart avatar Feb 24 '16 22:02 blowdart

Okay, cum si cum sa. I understand that I should be able to. However, it is not clear how to.

So I create a method to build the desired policies:

public AuthorizationPolicies BuildPolicies()

{

        AuthorizationOptions options = new AuthorizationOptions();

        options.AddPolicy("Administrators", policy =>

        {

            policy.RequireRole("Administrator");

        });

        options.AddPolicy("Bidder", policy =>

        {

            policy.RequireRole("Tester");

        });

        Return options;

}

Now, when I add

            Services.AddAuthorization requires a delegate, which the example lambda expressions meet.  So how do I now change the BuildPolicies so that it is delegate that will meet the structure requirements for the AddAuthorization.  I know it is probably simple, but just not seeing yet.

From: Barry Dorrans [mailto:[email protected]] Sent: Wednesday, February 24, 2016 5:57 PM To: aspnet/Security [email protected] Cc: Tgueth [email protected] Subject: Re: [Security] unable to configure Authorization policies at other than AddAuthorization (#712)

I don't see what's wrong with pulling policies from the database inside configure services, but I'd like to know what you want inside the database. Remember you can inject into your handler.

I'm guessing that your database may contain roles or permissions that change at runtime? If so, I'd inject the permissions repo into the handler itself, and pull them out at evaluation time.

— Reply to this email directly or view it on GitHub https://github.com/aspnet/Security/issues/712#issuecomment-188497915 . https://github.com/notifications/beacon/ADkfz14AUBrp05sj8y2E0YB6tFpi05BBks5pniy7gaJpZM4Hh-c-.gif

Tgueth avatar Feb 25 '16 02:02 Tgueth

I wouldn't do it with roles, I'd do it with code based requirements and handlers. That way you can access your repos inside the handler.

I realise this is a huge change from what we had before. I have a workshop you can work through which will help you move away from roles :) https://github.com/blowdart/AspNetAuthorizationWorkshop

blowdart avatar Feb 25 '16 03:02 blowdart

This is all doable in rc2 by implementing a policy provider. The current model is push only at startup time, overriding the policy provider gives you full control over the logic that manifests policies at runtime.

davidfowl avatar Feb 25 '16 04:02 davidfowl

I appreciate that I could change away from Roles, and may down the road. But it doesn’t matter which approach I take as basically the AddAuthorization requires the setup at when it is invoked (aka must be passed as a parameter). I am not using [Authorize] as I move. But ASP.NET5/MVC6 requires so many changes that I simply don’t need something else to rewrite.

Tom

From: Barry Dorrans [mailto:[email protected]] Sent: Wednesday, February 24, 2016 10:58 PM To: aspnet/Security [email protected] Cc: Tgueth [email protected] Subject: Re: [Security] unable to configure Authorization policies at other than AddAuthorization (#712)

I wouldn't do it with roles, I'd do it with code based requirements and handlers. That way you can access your repos inside the handler.

I realise this is a huge change from what we had before. I have a workshop you can work through which will help you move away from roles :) https://github.com/blowdart/AspNetAuthorizationWorkshop

— Reply to this email directly or view it on GitHub https://github.com/aspnet/Security/issues/712#issuecomment-188596238 . https://github.com/notifications/beacon/ADkfzwAAzrq1UEYkuxAxdm_ItuGP7M2dks5pnnNCgaJpZM4Hh-c-.gif

Tgueth avatar Feb 25 '16 04:02 Tgueth

Okay, so any idea when RC2 will be available?

Tom

From: David Fowler [mailto:[email protected]] Sent: Wednesday, February 24, 2016 11:20 PM To: aspnet/Security [email protected] Cc: Tgueth [email protected] Subject: Re: [Security] unable to configure Authorization policies at other than AddAuthorization (#712)

This is all doable in rc2 by implementing a policy provider. The current model is push only at startup time, overriding the policy provider gives you full control over the logic that manifests policies at runtime.

— Reply to this email directly or view it on GitHub https://github.com/aspnet/Security/issues/712#issuecomment-188607273 . https://github.com/notifications/beacon/ADkfz4EbUQe5AM4Hou9puspT45w3yfbXks5pnnhfgaJpZM4Hh-c-.gif

Tgueth avatar Feb 25 '16 04:02 Tgueth

Okay, I figured it out. I can put the method in the lamda expression as such:

services.AddAuthoriization( options => { BuildAuthorizationOptions(options);});

Tgueth avatar Feb 25 '16 11:02 Tgueth

Barry, first of all, thanks for your videos. Good stuff. Helping me out a lot. My scenario is similar, except they want to store the names of the policies in the DB and have the roles/claims stored in the db as well so when the app loads they are pulled dynamically, not hard coded. I'm not using Repos(bad developer, I know)...can I just use an dbContext somehow to pull them and then load them? Thanks, keep up the great work!

furrybaer avatar Dec 22 '16 19:12 furrybaer