Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing HostStrategy for login without tenant id #1071

Open
umutaktas opened this issue Dec 3, 2024 · 0 comments
Open

Changing HostStrategy for login without tenant id #1071

umutaktas opened this issue Dec 3, 2024 · 0 comments

Comments

@umutaktas
Copy link

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": ""
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant