Skip to content

Changing HostStrategy for login without tenant id #1071

@umutaktas

Description

@umutaktas

Hi,
I want change strategy for MutliTenant with WithHostStrategy. I added .WithHostStrategy("*.tenant.?") keyword like below and load tenant list from config file. but I get authentication error.
What is your suggetions?
@fretje @justSteve @pashaie @maxiar @geekz-reno @iammukeshm

regards

ArgumentNullException.ThrowIfNull(services);
_ = services.AddTransient<IConnectionStringValidator, ConnectionStringValidator>();
_ = services.BindDbContext<TenantDbContext>();
_ = services.AddMultiTenant<FshTenantInfo>(config =>
    {
        // to save database calls to resolve tenant
        // this was happening for every request earlier, leading to ineffeciency
        config.Events.OnTenantResolveCompleted = async (context) =>
        {
            if (context.MultiTenantContext.StoreInfo is null) return;
            if (context.MultiTenantContext.StoreInfo.StoreType != typeof(DistributedCacheStore<FshTenantInfo>))
            {
                var sp = ((HttpContext)context.Context!).RequestServices;
                var distributedCacheStore = sp
                    .GetService<IEnumerable<IMultiTenantStore<FshTenantInfo>>>()!
                    .FirstOrDefault(s => s.GetType() == typeof(DistributedCacheStore<FshTenantInfo>));

                _ = await distributedCacheStore!.TryAddAsync(context.MultiTenantContext.TenantInfo!);
            }
            await Task.FromResult(0);
        };
    })
   
    .WithHostStrategy("*.__tenant__.?")      
    // removed other strategies
    .WithConfigurationStore()  // load from config file
    .WithDistributedCacheStore(TimeSpan.FromMinutes(60))
    .WithEFCoreStore<TenantDbContext, FshTenantInfo>();

_ = services.AddScoped<ITenantService, TenantService>();
return services;

part of appseting.config file:

...
"Finbuckle:MultiTenant:Stores:ConfigurationStore": {
  "Tenants": [
    {
      "Id": "root",
      "Identifier": "root",
      "Name": "Root Tenant",
      "ConnectionString": ""
    },
    {
      "Id": "tenant2 ",
      "Identifier": "tenant2 ",
      "Name": "Second Tenant",
      "ConnectionString": ""
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions