编辑 - 我现在可以确认这个解决方案对我有用。
我正在为我自己的项目调查这个问题,该项目需要根据主机名或请求的第一个文件夹段支持多租户,具体取决于配置。
我尚未对此进行测试,但我认为在启动时使用类似这样的代码可能会奏效:
例如,我想为每个租户使用不同的 auth cokie 名称,并且我正在考虑在启动时使用类似这样的代码:
// for first folder segment represents the tenant
app.Map("/branch1", app1 =>
{
app1.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<SiteUserManager, SiteUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
CookieName = "branch1-app"
});
});
// for when the host name of the request identifies the tenant
app.MapWhen(IsDomain1, app2 =>
{
app2.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<SiteUserManager, SiteUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
},
CookieName = "domain1-app"
});
});
private bool IsDomain1(IOwinContext context)
{
return (context.Request.Host.Value == "domain1");
}