【发布时间】:2021-10-07 12:56:18
【问题描述】:
我想在 .NET Core 应用程序上进行基于租户的身份验证。我正在使用 AutoFac 构建基于租户的容器。
我能够创建ServiceCollection 和Populate 身份验证服务。但是,身份验证失败并为租户获得未经授权的响应。
public static MultitenantContainer ConfigureMultitenantContainer(IContainer container)
{
multitenantContainer.ConfigureTenant("80fdb3c0-5888-4295-bf40-ebee0e3cd8f3", containerBuilder =>
{
containerBuilder.RegisterType<DataService>().As<IDataService>().InstancePerDependency();
containerBuilder.RegisterInstance(new OperationIdService()).SingleInstance();
ServiceCollection tenantServices = new();
tenantServices.AddAuthentication(opt =>
{
opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
{
options.Authority = "https://key-cloak.cloudapp.azure.com:8443/auth/realms/test";
options.Audience = "test";
});
containerBuilder.Populate(tenantServices);
});
return multitenantContainer;
}
【问题讨论】:
-
您是否深入了解了 ASP.NET 方面的情况,例如,它是否只解析一次身份验证设置并进行缓存(因此您不会获得每个租户的信息)?可能不仅仅是您的 DI 设置导致不匹配。
-
也可能相关 -
Populatedoesn't just convert registrations, it also sets up some default things 用于与 Microsoft.Extensions.DependencyInjection 交互。您是否尝试过使用 Autofac 手动注册身份验证内容而不使用Populate?
标签: asp.net-core autofac