gpt4 book ai didi

c# - ASP.NET Core 2.0+ 中的 Multi-Tenancy

转载 作者:太空狗 更新时间:2023-10-30 01:13:37 27 4
gpt4 key购买 nike

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

问题:SaasKit 有一个 UsePerTenant 方法,它非常适合根据当前租户根据请求执行不同的操作。

我的目标是使用 UsePerTenant 方法结合通过依赖注入(inject)注入(inject)的不同 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 方法仅运行每次应用程序启动后,UsePerTenant 方法应在 Configure 方法中使用,该方法针对 ASP.NET 管道中的每个传入/传出请求运行。

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

最佳答案

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

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

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

如果您好奇,请在此处查看更多信息:https://www.finbuckle.com/MultiTenant

关于c# - ASP.NET Core 2.0+ 中的 Multi-Tenancy ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49244634/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com