ASP.NET Core 2.0+ 中的多租户

Multitenancy in ASP.NET Core 2.0+

背景: 我想在 ASP.NET Core 中开发一个多租户应用程序,并且一直在研究 Ben Fosters Saaskit 库,它似乎为多租户应用程序中的常见问题提供了很好的解决方案。

问题: SaasKit 有一个 UsePerTenant 方法,可以根据当前租户根据请求执行不同的操作。

我的目标是使用 UsePerTenant 方法结合通过依赖注入注入的不同 IOptions 对象。这可以用在像

这样的身份验证中间件中
AddAuthentication().AddCookie(..).AddOpenIdConnect(...)

在Startup.cs

中的ConfigureServices方法中配置
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)
    {
        ...
    }
}

我无法使 ASP.NET 2.0+ 中的身份验证中间件针对每个请求使用不同的 IOptions 对象,因为 Startup.cs 文件中的 ConfigureServices 方法仅运行每次应用程序启动后,应该在 Configure 方法中使用 UsePerTenant 方法,即 ASP.NET 管道中每个 incoming/outgoing 请求的 运行。

问题: 如何根据当前租户动态更改 ConfigureServices 方法中的 cookie 和 OpenID Connect 选项?

下面的PR针对上述问题提供了解决方案。 https://github.com/saaskit/saaskit/pull/96

PR 已经合并到 "master" 分支。

尚未合并(2018 年 11 月)

我找到了一个很好的方法来为任何类型的 ASP.NET 核心选项(包括 cookie 或 openID Connect)获取每个租户选项。我已将其包装到一个名为 Finbuckle.MultiTenant.

的框架中

基本上可以归结为如下所示的设置:

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

如果你好奇,请看我这里了解更多信息:https://www.finbuckle.com/MultiTenant