dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

Changing HostStrategy for login without tenant id

Open umutaktas opened this issue 1 year ago • 0 comments

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": ""
    }
  ]
}

umutaktas avatar Dec 03 '24 20:12 umutaktas