Save $100 with our Black Friday deals!

Get $100 OFF with promo code BLACKFRIDAY2024 at checkout until November 30.

See Pricing

Multi-tenancy

When ASP.NET Core Identity security is enabled Radzen Blazor Studio offers multi-tenancy support (not available in community version). Multi-tenancy allows single instance of Radzen application to serve multiple tenants.

How-to video

Create tenants

When multi-tenancy is enabled, Radzen Blazor Studio will seed tenantsadmin user (tenantsadmin/tenantsadmin login credentials) as part of security tables migrations (server/Data/Migrations folder) that can be used to add tenants runtime during development and when the application is deployed:

We strongly advise you to change this user password before deploying the application. You can do that from the profile page:

When you create tenants you can choose which tenant is active from the DataGrid “Switch tenant” button:

You can add, update and delete tenants.

Both tenant Name and Hosts are mandatory:

Hosts can be single or comma separated hosts where the application for this tenant will be deployed:

Once you have active tenant you can add roles and users for this tenant:

When role/user is created the active tenant is assigned automatically as tenant for the newly created role/user. Now when login with a user in development the application will use user tenant as active tenant.

Deploy to IIS

1. Create new radzen-rocks.com web site in your IIS with following bindings.

2. Modify your hosts (%WINDIR%\system32\drivers\etc\hosts) file to simulate hypothetical domains locally.

...
127.0.0.1       radzen-rocks.com
127.0.0.1       tenant1.radzen-rocks.com
127.0.0.1       tenant2.radzen-rocks.com
...

3. Deploy the application from Radzen Blazor Studio and browse tenants.

API

Radzen Blazor Studio generates a service called SecurityService and makes it available in all pages as the Security property. This service allows you to:

Get the current user tenant id

The User property is an instance of the ApplicationUser class.

var id = Security.User?.TenantId;

Get the current user tenant name

The ApplicationTenant property of the user is an instance of the ApplicationTenant class.

var name = Security.User?.ApplicationTenant.Name;

Filtering entities by tenant

Important: Radzen Blazor Studio will not generate by default any data filtering by tenant. The developer is responsible to filter or assign proper TenantId when query, create or update data.

To filter or associate desired entity by/with tenant you need to extend the entity with integer property that will hold the tenant Id and use OnXXXRead, OnXXXCreated and OnXXXUpdated partial service methods:

public partial class Order
{
    public int? TenantId { get; set; }
}
namespace <YourAppNamespace>
{
    public partial class NorthwindService
    {
        IHttpContextAccessor httpContextAccessor;
        ApplicationIdentityDbContext identityDbContext;
        int? TenantId;

        public NorthwindService(NorthwindContext context, NavigationManager navigationManager, IHttpContextAccessor httpContextAccessor, ApplicationIdentityDbContext identityDbContext)
        {
            this.httpContextAccessor = httpContextAccessor;
            this.context = context;
            this.navigationManager = navigationManager;
            this.identityDbContext = identityDbContext;

            var userId = httpContextAccessor.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
            var user = identityDbContext.Users.FirstOrDefault(u => u.Id == userId);
            if (user != null)
            {
                TenantId = user.TenantId;
            }
        }

        partial void OnOrdersRead(ref IQueryable<Order> orders)
        {
            orders = orders.Where(order => order.TenantId == TenantId);
        }

        partial void OnOrderUpdated(Order order)
        {
            order.TenantId = TenantId;
        }

        partial void OnOrderCreated(Order order)
        {
            order.TenantId = TenantId;
        }
    }
}