Search code examples
c#entity-framework-coremulti-tenantasp.net-core-2.0aspnetboilerplate

Multi-tenant authentication, IMustHaveTenant entity in ASP.NET Boilerplate Module Zero


Multi-Tenant Authentication

I have created a new tenant from the swagger UI as an admin and I can check in the data the tenant created successfully together with admin account.

Now how do I login as the admin of the newly created tenant?

Cos I tried Token-authenticating via Postman, specifying tenancyName in the request body and it only seems to authenticate the admin from default tenant - even when I put in rubbish in the tenancyName field, it won't detect any error or exception.

I check the configuration value in the CoreModule.cs and MultiTenancyEnabled has been set to true

enter image description here

Entity Relation with Tenant

Also I would like to relate one of my entity with the tenant entity. So I based the class on IMustHaveTenant interface along side FullAuditedEntity like this:

public class Rule: FullAuditedEntity, IMustHaveTenant
{
    public string columnA { get; set; }
    public string columnB { get; set; }
    public string columnC { get; set; }
    public int TenantId { get; set; }
}

Is it enough or do I have to further put in any codes in the DbContext?


Solution

  • Now how do I login as the admin of the newly created tenant?

    For Token Based Authentication, send the tenant id in the Abp.TenantId header.

    In Postman, click on Headers, add the Key as Abp.TenantId and the Value as your tenant id. If you just created a new tenant, that would be 2 as there is a Default tenant with id 1.

    Also I would like to relate one of my entity with the tenant entity. [...] Is it enough or do I have to further put in any codes in the DbContext?

    You have to add a DbSet in your DbContext:

    public class MyDbContext : AbpDbContext
    {
        public DbSet<Product> Products { get; set; }
    
        public MyDbContext(DbContextOptions<MyDbContext> options)
            : base(options)
        {
        }
    }