dazinator / Dotnettency

Mutlitenancy for dotnet applications
MIT License
111 stars 23 forks source link

Adding external identity provider #31

Closed RolandGuijt closed 6 years ago

RolandGuijt commented 6 years ago

I'm using 1.40 latest build. I'm adding external identity providers to the tenant specific container (for example AddGoogle, AddOpenIdConnect). When I (in a MVC controller) get the list of configured providers using an IAuthenticationSchemeProvider instance it seems nothing is configured.

Is there something special I should do?

dazinator commented 6 years ago

What asp.net core version are you using, 1, 1.1 or 2.0?

RolandGuijt commented 6 years ago

2.0

Op 21 jan. 2018 13:58 schreef "Darrell" notifications@github.com:

What asp.net core version are you using, 1, 1.1 or 2.0?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dazinator/Dotnettency/issues/31#issuecomment-359246607, or mute the thread https://github.com/notifications/unsubscribe-auth/AMOzJpBaIGv2O2DlztygAcNWg9PxwxKRks5tMzSQgaJpZM4RlwSu .

dazinator commented 6 years ago

Are you adding Mvc in the tenant container also? (And usemvc in tenant middleware pipeline?)

dazinator commented 6 years ago

Failing that, if you have time to point me to a repro that would really help.

RolandGuijt commented 6 years ago

Adding MVC to the tenant container and then using it in the tenant's pipeline doesn't work for me. I'm probably missing something here. See the startup below (without auth). It gives an exception when adding MVC to the pipeline: required services not found.

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            return services.AddMultiTenancy<Tenant>((multiTenancyOptions) =>
            {
                multiTenancyOptions
                    .DistinguishTenantsWith<AcrValuesTenantDistinguisherFactory>()
                    .InitialiseTenant<TenantShellFactory>()
                    .ConfigureTenantContainers((containerBuilder) =>
                    {
                        containerBuilder.WithStructureMap((organization, tenantServices) =>
                        {
                            tenantServices.AddMvc();
                        });
                    }).ConfigureTenantMiddleware((tenantsMiddlewareBuilder) =>
                    {
                        tenantsMiddlewareBuilder.OnInitialiseTenantPipeline((context, appBuilder) =>
                        {
                            appBuilder.UseMvc(routes =>
                            {
                                routes.MapRoute(
                                    name: "default",
                                    template: "{controller=Home}/{action=Index}/{id?}");
                            });
                        });
                    });
            });
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {

            app.UseStaticFiles();

            app.UseMultitenancy<Tenant>((options) =>
            {
                options.UsePerTenantContainers();
                options.UsePerTenantMiddlewarePipeline();
            });
        }
    }
dazinator commented 6 years ago

Hmm cant see anything obvious. I'll try and get a better sample up and running with mvc, cookie auth and asp.net core 2.0 but it may not be for a few days or so!

RolandGuijt commented 6 years ago

​That would be great.

I have an update: The supplied MVC example in the Dotnettency source (using ASP.NET Core 1) also doesn't work.

When I add: ​tenantServices.AddScoped<IActionContextAccessor, ActionContextAccessor>();

It starts up successfully but it can't find any pages (404 on every request)

With kind regards/ Met vriendelijke groeten

Roland Guijt Microsoft MVP Microsoft MCT Pluralsight author rolandguijt.com

2018-01-23 1:57 GMT+01:00 Darrell notifications@github.com:

Hmm cant see anything obvious. I'll try and get a better sample up and running with mvc, cookie auth and asp.net core 2.0 but it may not be for a few days or so!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dazinator/Dotnettency/issues/31#issuecomment-359630152, or mute the thread https://github.com/notifications/unsubscribe-auth/AMOzJhQ-0X0yk3QaY0U339Ia6aSJTE6wks5tNS5egaJpZM4RlwSu .

RolandGuijt commented 6 years ago

Any progress?

With kind regards/ Met vriendelijke groeten

Roland Guijt Microsoft MVP Microsoft MCT Pluralsight author rolandguijt.com

2018-01-23 14:02 GMT+01:00 Roland Guijt roland.guijt@gmail.com:

​That would be great.

I have an update: The supplied MVC example in the Dotnettency source (using ASP.NET Core 1) also doesn't work.

When I add: ​tenantServices.AddScoped<IActionContextAccessor, ActionContextAccessor>();

It starts up successfully but it can't find any pages (404 on every request)

With kind regards/ Met vriendelijke groeten

Roland Guijt Microsoft MVP Microsoft MCT Pluralsight author rolandguijt.com

2018-01-23 1:57 GMT+01:00 Darrell notifications@github.com:

Hmm cant see anything obvious. I'll try and get a better sample up and running with mvc, cookie auth and asp.net core 2.0 but it may not be for a few days or so!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dazinator/Dotnettency/issues/31#issuecomment-359630152, or mute the thread https://github.com/notifications/unsubscribe-auth/AMOzJhQ-0X0yk3QaY0U339Ia6aSJTE6wks5tNS5egaJpZM4RlwSu .

dazinator commented 6 years ago

Sorry, I was making progress and then nuget.org crapped up the other night and halted me. Have been super busy since but I will get back on this hopefully this week/end

durmisi commented 6 years ago

Is there any progress being made on this?

dazinator commented 6 years ago

It's next on my agenda.. Have been spread a bit thin working on other projects with a higher priority.

durmisi commented 6 years ago

Thanks

amalabey commented 6 years ago

This issue is blocking us from using dotnettency in our project. Can you give us some tips on where to look at?

dazinator commented 6 years ago

@amalabey A lot of stuff changed in asp.net core 2.0 to do with authentication and identity. Can you give me some specifics about what you are wanting to accomplish? If I could point you at a sample to help you with your use case, what would the sample show?

I have some very basic samples for asp.net core 2.0 here which may help: https://github.com/dazinator/Dotnettency.Samples/tree/aspnetcore20/src

However if you are wanting to use this for an asp.net core 2.0 project right now, then I am afraid it might be a little premature - I am hoping to find some more time to work on the project but it hasn't happened yet!

amalabey commented 6 years ago

Thanks for your quick response! I am trying to develop a multi-tenant app that supports multiple logical tenants identified by the subdomain in the url. Each tenant has its own database. The application should support yammer (and other providers too) but the yammer options (clientid, secret) are different for each tenant. So, need to be able to specify different yammer app settings for each tenant. Below is what I tried (with version 1.3.2).

  1. Register yammer auth in tenant container config, I get "No authentication handler is configured to handle the scheme" error.

  2. If I move AddMvc to tenant container config and UseMvc to tenant pipeline config, then I get "The constructor should not be static\r\nParameter name: constructor\r\n" error.

Btw, you've done a great job. I am still getting myself familiarize with your work. Happy to contribute in the future.

amalabey commented 6 years ago

Here's a sample with the issue reproduced. Note that I am using 1.3.2 with ASP.NET Core 2 here.

durmisi commented 6 years ago

Is there any progress being made on this? This issue is blocking me from using dotnettency in my project. Can you give some tips on where to look at?

dazinator commented 6 years ago

Just about to look at this now using the sample that @amalabey provided.

Like most of us, I don't get much time to spare these days, so If you are having an issue what really helps is if you can provide a repro / sample showing the problem and a quick explaination and I'll do my best to turn it around as quick as possible.

By the way I have just added a couple more asp.net core 2.0 - EF Core samples showing two approaches to multitenancy and efcore:

You can find those samples here: https://github.com/dazinator/Dotnettency.Samples/tree/aspnetcore20/src You can basically just run those projects and hit them on localhost:5000-5004 on /Blogs and /AddBlog to see what happens.

durmisi commented 6 years ago

@dazinator thanks for your answer. Could you please take a look on this repo https://github.com/amalabey/Dotnettency.Sample.Core2Auth/tree/master/src/Sample.Core2Auth where this issue is reproduced. The problem is that MVC doesn`t work when we use TenantContainers and register it per Tenant.

dazinator commented 6 years ago

I will begin looking at MVC per tenant container issue this evening. Can't promise a quick turnaround im afraid though!

dazinator commented 6 years ago

So for some reason, when AddMvc() is called on the tenant container, and then that container is used to resolve a type like MvcRouteHandler - structuremap's AspNetConstructorSelector says it can't find an appropriate constructor - because it thinks services required by the constructor are not registered.

When working at the root container level, AspNetConstructorSelector sees everything fine.

I am not an expert with structure map and it looks complicated. I don't expect this to be turned round very quickly i'm afraid, unless someone can lend a hand.

It's perhaps something I am doing wrong with sturcture map, or perhaps its a bug in AspNetConstructorSelector which is only seen in child containers.

My next plan of attack is to create a replication test with structure map - basically to proof if there is an issue with AspNetConstructorSelector. If there is I will raise an issue over there. Otherwise hopefully this will help narrow down the problem.

dazinator commented 6 years ago

An update on this. I have a new sample showing how Authentication can be configured for different tenants (I'm using cookie authentication)

https://github.com/dazinator/Dotnettency.Samples/tree/aspnetcore20/src/Sample.Authentication

This took a while because whilst putting it together I had to get to the bottom of a few issues and find workarounds.

  1. I raised an issue with StructureMap which is still currently unresolved: https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection/issues/43

  2. I have published Dotnettency.Container.Autofac package. The sample above uses this instead of structuremap, because I was able to get Autofac working.

Please can you you let me know if this helps address your scenarios now - if not, what would you like to see. Thanks!

RolandGuijt commented 6 years ago

Darrell,

This is great news! It seems to be exactly what I need. I'll try it out in one of my projects and I'll let you know how it went.

Thanks a lot!

Op di 28 aug. 2018 om 15:45 schreef Darrell notifications@github.com:

An update on this. I have a new sample showing how Authentication can be configured for different tenants (I'm using cookie authentication)

https://github.com/dazinator/Dotnettency.Samples/tree/aspnetcore20/src/Sample.Authentication

This took a while because whilst putting it together I had to get to the bottom of a few issues and find workarounds.

1.

I raised an issue with StructureMap which is still currently unresolved: structuremap/StructureMap.Microsoft.DependencyInjection#43 https://github.com/structuremap/StructureMap.Microsoft.DependencyInjection/issues/43 2.

I have published Dotnettency.Container.Autofac package. The sample above uses this instead of structuremap, because I was able to get Autofac working.

Please can you you let me know if this helps address your scenarios now - if not, what would you like to see. Thanks!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/dazinator/Dotnettency/issues/31#issuecomment-416589295, or mute the thread https://github.com/notifications/unsubscribe-auth/AMOzJpc6iScXL1I0WcUw2iRDgSneBalaks5uVUlvgaJpZM4RlwSu .

dazinator commented 6 years ago

@RolandGuijt - thank you. Also, I have a per tenant RazorPages example, where AddMvc() and UseMvc() are at the per tenant level: https://github.com/dazinator/Dotnettency.Samples/tree/aspnetcore20/src/Sample.RazorPages

It's a very basic sample, not using many MVC features or anything but still.. I haven't actually tried configuring MVC or Razor differently per tenant, they all have the same configuration in the sample. But I am assuming you could do stuff like.. Configure one tenant to pick up Razor pages from one additional folder, or include additional compilation references, tag helpers, application parts etc. I am hoping this might enable a few more interesting mvc configuration scenarios.

dazinator commented 6 years ago

I will close this for now, we can reopen new issue if there is any feedback.