Jonas
Jonas

Reputation: 3283

Multitenancy in ASP.NET Core 2.0+

Bakground: I want to develop a multi-tenant application in ASP.NET Core and have been looking into Ben Fosters Saaskit library which seems to provide good solutions for common problems in multitenancy applications.

Problem: The SaasKit have a UsePerTenant method which is nice for doing different things per-request depending on current tenant.

My goal is to use the UsePerTenant method combined with different IOptions objects injected via dependency injection. This can be used in the authentication middleware like

AddAuthentication().AddCookie(..).AddOpenIdConnect(...)

Which is configured in the ConfigureServices method in Startup.cs

public class Startup
{
    // Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        ...
    }

    // Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app)
    {
        ...
    }
}

I can’t make the authentication middleware in ASP.NET 2.0+ use different IOptions objects per-request since the ConfigureServices method in the Startup.cs file only runs once every application startup and the UsePerTenant method should be used in the Configure method which is running for each incoming/outgoing request in the ASP.NET pipeline.

Question: How to dynamically change cookie and OpenID Connect options in the ConfigureServices method based on current tenant?

Upvotes: 4

Views: 5120

Answers (2)

Jonas
Jonas

Reputation: 3283

The following PR provides a solution for the above question. https://github.com/saaskit/saaskit/pull/96

The PR have been merged with the "master" branch now.

It wasn't merged yet (November 2018)

Upvotes: 2

user914399
user914399

Reputation:

I have found a good way to get per tenant options for any type of ASP.NET Core options, including cookie or openID Connect. I have wrapped this up into a framework called Finbuckle.MultiTenant.

It basically boils down to a setup that looks like this:

services.AddMultiTenant().
            WithInMemoryStore()).
            WithRouteStrategy().
            WithPerTenantOptionsConfig<CookieAuthenticationOptions>((o, tenantContext) => o.Cookie.Name += tenantContext.Id);

See my here for more information if you are curious: https://www.finbuckle.com/MultiTenant

Upvotes: 3

Related Questions