General question about multi tenancy in Radzen

This may be a very basic question but I am a bit confused by the docs about multi tenancy.

So assume I have a blazor web app running on www.myapp.com

This app has many clients each with many users.

So each client is basically a tenant, right?

But in order to create a tenant I need to add host information, but in this case doesn't every tenant use the same host www.myapp.com?

Or do I need to add subdomain for each client like client1.myapp.com?

Maybe this is not even strictly related to Radzen but any help would be very much appreciated.

There are different implementations of multi-tenancy, more details about our implementation can be found here:

Thanks. Let me rephrase the question: in your implementation - can multiple tenants have the same host? From the link you shared I think so, but confirmation would be nice. Thanks

Indeed there is no such restriction.

Okay so I created two tenants. Tenant1 and Tenant2. Both have the same host "myapp.com". Each tenant has one user, user1 (tenant1) and user2 (tenant2).

When logging in I think this code is called:

        private ApplicationTenant GetTenant()
        {
            var tenants = Context.Set<ApplicationTenant>().ToList();

            var host = httpContextAccessor.HttpContext.Request.Host.Value;

            return tenants.Where(t => t.Hosts.Split(',').Where(h => h.Contains(host)).Any()).FirstOrDefault();
        }

So now when user2 wants to login, GetTenant() will always return tenant1 and user2 does not belong to tenant1 so logging in is not possible?

Tenants and Users are created when logged with tenantsadmin - it's up to you what values you will assign. If you have same host for different user/tenant combination indeed this code will return the first tenant.

Okay, but if I understand correctly this means that two tenants cannot have same host because in that case always the first tenant will be returned?

So basically I have to add a new subdomain for every tenant?

Yes, that's the example in our documentation article I've referred.

I think in your example here Multi-tenancy | Radzen Blazor Studio

tenant2user will not be able to login when navigating to localhost:5001 because tenant1 and tenant2 share the same host (localhost:5001) and therefore always tenant1 will be returned.

When logged with tenantsadmin the tenant is switched manually:

Yes I understand. But try logging in with tenantUser2 when on localhost:5001. will not be possible because GetTenant() will always return tenant1.

Or in your case radzen-rocks.com.

Your tenants have hosts like this:

tenant1: localhost:5001, tenant1.radzen-rocks.com
tenant2: localhost:5001, tenant2.radzen-rocks.com

So when user of tenant2 goes to localhost:5001 he cannot login because the code will always return tenant1

You are right, maybe we should update the code to provide better user discovery during development:

        public override async Task<ApplicationUser> FindByNameAsync(string normalizedName, CancellationToken cancellationToken = default)
        {
#if DEBUG
            return await base.FindByNameAsync(normalizedName, cancellationToken);
#else
            if (normalizedName.ToLower() == "tenantsadmin")
            {
                return await base.FindByNameAsync(normalizedName, cancellationToken);
            }

            var tenant = GetTenant();
            ApplicationUser user = null;

            if (tenant != null)
            {
                user = await Context.Set<ApplicationUser>().SingleOrDefaultAsync(r => r.NormalizedUserName == normalizedName && r.TenantId == tenant.Id, cancellationToken);
            }

            return user;
#endif
        }

So just be sure, you are saying that in production it is a requirement that each tenant has own defined host (e.g. client1.myapp.com) and hosts cannot be shared accross tenants, correct?

Yes, that’s the requirement.

1 Like